About Me

My Photo
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.


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.


Happy coding,


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

    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

  2. This comment has been removed by the author.

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

  4. This comment has been removed by the author.

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

  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);

  7. Hi

    How can we add points with bullets and numbers?

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

  9. is there any way to merge columns ?

  10. For instance Cheap D3 Gold, if you're a leading man in the Sixtieth level, but also in any section regarding Twentieth level GW2 Gold, the sport dynamically lowers your figures in order to main character Twenty first degree (most within + A single).

  11. Have you created a table with "colspan" or "rowspan"?

  12. My IDE (VS2010) is having trouble recognizing the First() function after Paragraphs.

    I have an insert but no inserttext or first. Anything I might be doing wrong?

  13. How ca I insert table in specific position or insted of text

  14. hey cathals... thanks for the library it help us here! if you have some spare time... please visit our place here in Asia, Indonesia >> Bandung city


  15. This is a great example! Thanks! It doesn't look like the text is horizontally aligned in your picture like your code says it should be. I am having the same issue with my code :(

  16. Hi,

    Nice examples.

    I want to pick a text box from an ms word document (2013) and convert it to an image using C#. How can i go about that?

  17. Cool simple library, but i'm missing the feature to AppendTable in a table/cell

  18. How to set vertical orientation for text in table?

  19. How to add Numbered List inside Table Cell ?

  20. Nice article..

    Can we check whether there is col span in table using docx package?

  21. Hi, when I insert an image into a table cell and save the document, it becomes corrupted, and if you try to open it it shows the error message : uncpecified error location part /word/header2.xml
    however the file can be opened after recovery.

    how to avoid this error message ? this error happens whenever you insert an image. Otherwise everything is ok with the file. Any clues to the solution would be highly appreciated. Thanks.

  22. How to add table in a numbered list. Is this library still maintained?

  23. how I can add table at specific bookmark ?