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

Thursday, June 10, 2010

Fluid Paragraph creation with Append

DocX v1.0.0.9 makes Paragraph creation even easier. The original idea of using Append in this manor came from a DocX user Morten Bjerre. Once again Morten I would like to say thank you for your contribution.

The below code creates a document with the following content.

  1. A Picture
  2. A Hyperlink
  3. Different fonts
  4. Different text styles

I hope you will agree that Append() and its variants [AppendLine(), AppendPicture() and AppendHyperlink()] make the creation of Paragraphs flow in a very fluid manor.

The below code creates the following output.

Untitled

Code Snippet
  1. // Create a document.
  2.             using (DocX document = DocX.Create("Test.docx"))
  3.             {
  4.                 // Add a hyperlink into the document.
  5.                 Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));
  6.  
  7.                 // Add a Table into the document.
  8.                 Table table = document.AddTable(2, 2);
  9.                 table.Design = TableDesign.ColorfulGridAccent2;
  10.                 table.Alignment = Alignment.center;
  11.                 table.Rows[0].Cells[0].Paragraphs[0].Append("3");
  12.                 table.Rows[0].Cells[1].Paragraphs[0].Append("1");
  13.                 table.Rows[1].Cells[0].Paragraphs[0].Append("4");
  14.                 table.Rows[1].Cells[1].Paragraphs[0].Append("1");
  15.  
  16.                 // Add an image into the document.    
  17.                 Novacode.Image image = document.AddImage("Image.jpg");
  18.  
  19.                 // Create a picture (A custom view of an Image).
  20.                 Picture picture = image.CreatePicture();
  21.                 picture.Rotation = 10;
  22.                 picture.SetPictureShape(BasicShapes.cube);
  23.  
  24.                 // Insert a new Paragraph into the document.
  25.                 Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
  26.                 title.Alignment = Alignment.center;
  27.  
  28.                 // Insert a new Paragraph into the document.
  29.                 Paragraph p1 = document.InsertParagraph();
  30.  
  31.                 // Append content to the Paragraph
  32.                 p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
  33.                 p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append(".");
  34.                 p1.AppendLine();
  35.                 p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
  36.                 p1.AppendLine();
  37.                 p1.AppendLine("Can you check this Table of figures for me?");
  38.                 p1.AppendLine();
  39.                 
  40.                 // Insert the Table after Paragraph 1.
  41.                 p1.InsertTableAfterSelf(table);
  42.  
  43.                 // Insert a new Paragraph into the document.
  44.                 Paragraph p2 = document.InsertParagraph();
  45.  
  46.                 // Append content to the Paragraph.
  47.                 p2.AppendLine("Is it correct?");
  48.  
  49.                 // Save this document.
  50.                 document.Save();
  51.             }

If you would like to request a new feature, report a bug or just say hi, send me an email at coffey.cathal@gmail.com

Happy coding,
Cathal

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.

3 comments:

  1. I love it,Excellent article.I am decide to put this into use one of these days.Thank you for sharing this.To Your Success!
    _____________________________________________________________________________

    Rc Helicopter Parts|Rc Helicopter|Mini Rc Helicopter

    ReplyDelete
  2. The perfect!These articles written too great,they rich contents and data accurately.they are help to me.I expect to see your new share.
    -----------------
    RS Gold Runescape Gold Buy WOW Gold

    ReplyDelete
  3. I think there's a bug with the RowCount and ColumnCount property. After creating the table, I tried to iterate through the Row & Column collection, but if you rely on the count properties, they only return 0.

    ReplyDelete