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

Friday, May 28, 2010

DocX and Tables

Create and inserting Tables into documents is easy with DocX.

Simple Table
  1. // Create a document.
  2. using (DocX document = DocX.Create(@"Test.docx"))
  3. {
  4. // Add a Table to this document.
  5. Table t = document.AddTable(2, 3);
  6. // Specify some properties for this Table.
  7. t.Alignment = Alignment.center;
  8. t.Design = TableDesign.MediumGrid1Accent2;
  9. // Add content to this Table.
  10. t.Rows[0].Cells[0].Paragraphs.First().Append("A");
  11. t.Rows[0].Cells[1].Paragraphs.First().Append("B");
  12. t.Rows[0].Cells[2].Paragraphs.First().Append("C");
  13. t.Rows[1].Cells[0].Paragraphs.First().Append("D");
  14. t.Rows[1].Cells[1].Paragraphs.First().Append("E");
  15. t.Rows[1].Cells[2].Paragraphs.First().Append("F");
  16. // Insert the Table into the document.
  17. document.InsertTable(t);
  18. document.Save();
  19. }// Release this document from memory.

The above code will creates a document that looks like the below image.

Untitled

Paragraphs inside Table Cells can of course contain content such as Images, Hyperlinks and custom text styles as in the below example.

Advance Table
  1. // Create a document.
  2. using (DocX document = DocX.Create(@"Test.docx"))
  3. {
  4. // Add a Table to this document.
  5. Table table = document.AddTable(2, 3);
  6. // Add a Hyperlink into this document.
  7. Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com"));
  8. // Add an Image into this document.
  9. Novacode.Image i = document.AddImage(@"Logo.png");
  10. // Create a Picture (Custom View) of this Image.
  11. Picture p = i.CreatePicture();
  12. p.Rotation = 10;
  13. // Specify some properties for this Table.
  14. table.Alignment = Alignment.center;
  15. table.Design = TableDesign.LightShadingAccent2;
  16. // Insert the Table into the document.
  17. Table t1 = document.InsertTable(table);
  18. // Add content to this Table.
  19. t1.Rows[0].Cells[0].Paragraphs.First().AppendHyperlink(h).Append(" is my favourite search engine.");
  20. t1.Rows[0].Cells[1].Paragraphs.First().Append("This text is bold.").Bold();
  21. t1.Rows[0].Cells[2].Paragraphs.First().Append("Underlined").UnderlineStyle(UnderlineStyle.singleLine);
  22. t1.Rows[1].Cells[0].Paragraphs.First().Append("Green").Color(Color.Green);
  23. t1.Rows[1].Cells[1].Paragraphs.First().Append("Right to Left").Direction = Direction.RightToLeft;
  24. t1.Rows[1].Cells[2].Paragraphs.First().AppendPicture(p);
  25. document.Save();
  26. }// Release this document from memory.

The above code will creates a document that looks like the below image.

Untitled2

Happy coding,
Cathal