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

12 comments:

  1. how can i set a row header?
    I have to repeat a row every page.
    thanks

    ReplyDelete
    Replies
    1. Once again great post. You seem to have a good understanding of these themes.When I entering your blog,I felt this . Come on and keep writting your blog will be more attractive. To Your Success!
      -----------------
      RS Gold Runescape Gold Buy WOW Gold

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Great post Corners :-bd
    I can add row dynamically

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

    ReplyDelete
  5. Is there a w ay to add cells to the rows dynamically ?

    ReplyDelete
  6. // Loop through the rows in the Table and insert data from the data source.
    for (int row = 1; row < invoice_table.RowCount; row++)
    {
    for (int cell = 0; cell < invoice_table.Rows[row].Cells.Count; cell++)
    {
    Paragraph cell_paragraph = invoice_table.Rows[row].Cells[cell].Paragraphs[0];
    cell_paragraph.InsertText(data.Rows[row - 1].ItemArray[cell].ToString(), false);
    }
    }

    ReplyDelete
  7. Hi

    How can we add points with bullets and numbers?

    ReplyDelete
  8. Is there any way to find a specific table in an existing document and to add rows to it?

    ReplyDelete
  9. is there any way to merge columns ?

    ReplyDelete