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
- // Create a document.
- using (DocX document = DocX.Create(@"Test.docx"))
- {
- // Add a hyperlink to this document.
- Hyperlink h = document.AddHyperlink
- ("Google", new Uri("http://www.google.com"));
- // Add a new Paragraph to this document.
- Paragraph p = document.InsertParagraph();
- p.Append("My favourite search engine is ");
- p.AppendHyperlink(h);
- p.Append(", I think it's great.");
- // Save all changes made to this document.
- document.Save();
- }
Output
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
- The Document class now contains SetDirection(Direction)
- The Table class now contains SetDirection(Direction)
- The Row class now contains SetDirection(Direction)
- The Cell class now contains SetDirection(Direction)
Where Direction is an enum with the choices LeftToRight and RightToLeft.
Example
- // Create a new document.
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Create a new Paragraph with the text "Hello World".
- Paragraph p = document.InsertParagraph("Hello World.");
- // Make this Paragraph flow right to left. Default is left to right.
- p.Direction = Direction.RightToLeft;
- // Save all changes made to this document.
- document.Save();
- }
Output
3) Indentation
The Paragraph class now contains four properties for setting Indentation.
- IndentationFirstLine(float) which indents only the first line of a paragraph.
- IndentationHanging(float) which indents all but the first line of a paragraph.
- IndentationBefore(float) which indents before content.
- IndentationAfter(float) which indents after content.
Example
- // Create a new document.
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Create a new Paragraph.
- Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
- // Indent only the first line of the Paragraph.
- p.IndentationFirstLine = 1.0f;
- // Save all changes made to this document.
- document.Save();
- }
Output
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
- using (DocX document = DocX.Create("Test.docx"))
- {
- // Add an image to the document.
- Image i = document.AddImage(@"Image.jpg");
- // Create a picture i.e. (A custom view of an image)
- Picture p = i.CreatePicture();
- p.FlipHorizontal = true;
- p.Rotation = 10;
- // Create a new Paragraph.
- Paragraph par = document.InsertParagraph();
- // Append content to the Paragraph.
- par.Append("Here is a cool picture")
- .AppendPicture(p)
- .Append(" don't you think so?");
- // Save all changes made to this document.
- document.Save();
- }
Output
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.
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?
ReplyDeleteAgree with this issue. Can I make link to a bookmark?
DeleteFirst 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.
DeleteI 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.
Do you have an example of using an internal hyperlink or linking to a bookmark?
ReplyDeleteThis comment has been removed by the author.
ReplyDelete