About Me

My photo
Ireland
Hello, my name is Cathal Coffey. I am best described as a hybrid between a developer and an adventurer. When I am not behind a keyboard coding, I am hiking and climbing the beautiful mountains of my home country Ireland. I am a full time student studying Computer Science & Software Engineering at the National University of Ireland Maynooth. I am finishing the final year of a 4 year degree in September 2009. I am the creator of an open source project on codeplex.com called DocX. At the moment I spend a lot of my free time advancing DocX and I enjoy this very much. My aim is to build a community around DocX and add features based on requests from this community. I really enjoy hearing about how people are using DocX in their work\personal projects. So if you are one of these people, please send me an email. Cathal coffey.cathal@gmail.com

Wednesday, June 9, 2010

DocX version 1.0.0.9

The summer is finally here and so is DocX version 1.0.0.9. I would like to say sorry once again for the long wait but I simply couldn’t allow DocX to effect my final year college exams.

I received lots of feature requests over the past few months. I intend to implement as many of these features as possible over the next few releases. I have decided to make many small releases instead of one large release.

So what's new in this release?

1) Hyperlinks

DocX now supports hyperlinks.

The Document class now contains AddHyperlink(string, Uri)
The Paragraph class now contains AppendHyperlink(Hyperlink);

Example

Code Snippet
  1. // Create a document.
  2. using (DocX document = DocX.Create(@"Test.docx"))
  3. {
  4.     // Add a hyperlink to this document.
  5.     Hyperlink h = document.AddHyperlink
  6.     ("Google", new Uri("http://www.google.com"));
  7.  
  8.     // Add a new Paragraph to this document.
  9.     Paragraph p = document.InsertParagraph();
  10.     p.Append("My favourite search engine is ");
  11.     p.AppendHyperlink(h);
  12.     p.Append(", I think it's great.");
  13.  
  14.     // Save all changes made to this document.
  15.     document.Save();
  16. }

Output

clip_image002[4]

2) Content Direction

The Paragraph class now contains a property called Direction which can be either LeftToRight or RightToLeft. This property has both a getter and setter function.

To accommodate Paragraph Direction

  1. The Document class now contains SetDirection(Direction)
  2. The Table class now contains SetDirection(Direction)
  3. The Row class now contains SetDirection(Direction)
  4. The Cell class now contains SetDirection(Direction)

Where Direction is an enum with the choices LeftToRight and RightToLeft.

Example

Code Snippet
  1. // Create a new document.
  2. using (DocX document = DocX.Create("Test.docx"))
  3. {
  4.     // Create a new Paragraph with the text "Hello World".
  5.     Paragraph p = document.InsertParagraph("Hello World.");
  6.  
  7.     // Make this Paragraph flow right to left. Default is left to right.
  8.     p.Direction = Direction.RightToLeft;
  9.  
  10.     // Save all changes made to this document.
  11.     document.Save();
  12. }

Output

clip_image002[6]

3) Indentation

The Paragraph class now contains four properties for setting Indentation.

  1. IndentationFirstLine(float) which indents only the first line of a paragraph.
  2. IndentationHanging(float) which indents all but the first line of a paragraph.
  3. IndentationBefore(float) which indents before content.
  4. IndentationAfter(float) which indents after content.

Example

Code Snippet
  1. // Create a new document.
  2. using (DocX document = DocX.Create("Test.docx"))
  3. {
  4.     // Create a new Paragraph.
  5.     Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
  6.  
  7.     // Indent only the first line of the Paragraph.
  8.     p.IndentationFirstLine = 1.0f;
  9.  
  10.     // Save all changes made to this document.
  11.     document.Save();
  12. }

Output

clip_image002[8]

4) Pictures

The Image class now contains a CreatePicture() function and the Paragraph class contains a AppendPicture(Picture) function. The below example should explain how these updates enable easier Paragraph building.

Example

Code Snippet
  1. using (DocX document = DocX.Create("Test.docx"))
  2. {
  3.     // Add an image to the document.
  4.     Image i = document.AddImage(@"Image.jpg");
  5.  
  6.     // Create a picture i.e. (A custom view of an image)
  7.     Picture p = i.CreatePicture();
  8.     p.FlipHorizontal = true;
  9.     p.Rotation = 10;
  10.  
  11.     // Create a new Paragraph.
  12.     Paragraph par = document.InsertParagraph();
  13.  
  14.     // Append content to the Paragraph.
  15.     par.Append("Here is a cool picture")
  16.        .AppendPicture(p)
  17.        .Append(" don't you think so?");
  18.  
  19.     // Save all changes made to this document.
  20.     document.Save();
  21. }

Output

clip_image002[10]

Future

As always, I offer this code to you for free. I am however a student and if you would like to help me pay of some of my student debt , you can make a donation to DocX via paypal.

5 comments:

  1. Hey, great stuff - do you know if it's possible to link to other paragraphs in the document? In the top of my document I create an overview of some elements later in the document. I'd like to link to these parts later in the document, but I don't know if this is possible?

    ReplyDelete
    Replies
    1. Agree with this issue. Can I make link to a bookmark?

      Delete
    2. First of all thank you very much for all your effort and for sharing this amazing piece of work, you really make a difference in how things are done now in programming, congratulations.
      I have the same question, is it possible to make link to an element inside the same document? Like images or other paragraphs?

      Thanks in advance.

      Delete
  2. Do you have an example of using an internal hyperlink or linking to a bookmark?

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete