Create and inserting Tables into documents is easy with DocX.
Simple Table
- // Create a document.
- using (DocX document = DocX.Create(@"Test.docx"))
- {
- // Add a Table to this document.
- Table t = document.AddTable(2, 3);
- // Specify some properties for this Table.
- t.Alignment = Alignment.center;
- t.Design = TableDesign.MediumGrid1Accent2;
- // Add content to this Table.
- t.Rows[0].Cells[0].Paragraphs.First().Append("A");
- t.Rows[0].Cells[1].Paragraphs.First().Append("B");
- t.Rows[0].Cells[2].Paragraphs.First().Append("C");
- t.Rows[1].Cells[0].Paragraphs.First().Append("D");
- t.Rows[1].Cells[1].Paragraphs.First().Append("E");
- t.Rows[1].Cells[2].Paragraphs.First().Append("F");
- // Insert the Table into the document.
- document.InsertTable(t);
- document.Save();
- }// 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
- // Create a document.
- using (DocX document = DocX.Create(@"Test.docx"))
- {
- // Add a Table to this document.
- Table table = document.AddTable(2, 3);
- // Add a Hyperlink into this document.
- Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com"));
- // Add an Image into this document.
- Novacode.Image i = document.AddImage(@"Logo.png");
- // Create a Picture (Custom View) of this Image.
- Picture p = i.CreatePicture();
- p.Rotation = 10;
- // Specify some properties for this Table.
- table.Alignment = Alignment.center;
- table.Design = TableDesign.LightShadingAccent2;
- // Insert the Table into the document.
- Table t1 = document.InsertTable(table);
- // Add content to this Table.
- t1.Rows[0].Cells[0].Paragraphs.First().AppendHyperlink(h).Append(" is my favourite search engine.");
- t1.Rows[0].Cells[1].Paragraphs.First().Append("This text is bold.").Bold();
- t1.Rows[0].Cells[2].Paragraphs.First().Append("Underlined").UnderlineStyle(UnderlineStyle.singleLine);
- t1.Rows[1].Cells[0].Paragraphs.First().Append("Green").Color(Color.Green);
- t1.Rows[1].Cells[1].Paragraphs.First().Append("Right to Left").Direction = Direction.RightToLeft;
- t1.Rows[1].Cells[2].Paragraphs.First().AppendPicture(p);
- document.Save();
- }// Release this document from memory.
The above code will creates a document that looks like the below image.
Happy coding,
Cathal
how can i set a row header?
ReplyDeleteI have to repeat a row every page.
thanks
This comment has been removed by the author.
ReplyDeleteGreat post Corners :-bd
ReplyDeleteI can add row dynamically
This comment has been removed by the author.
ReplyDeleteThanks!
ReplyDeleteIs there a w ay to add cells to the rows dynamically ?
ReplyDelete// Loop through the rows in the Table and insert data from the data source.
ReplyDeletefor (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);
}
}
Hi
ReplyDeleteHow can we add points with bullets and numbers?
How can we add points with bullets and Launch x431,launch x431 scanner,launch x431 gds numbers?
ReplyDeleteIs there any way to find a specific table in an existing document and to add rows to it?
ReplyDeleteis there any way to merge columns ?
ReplyDeleteFor 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).
ReplyDeleteHave you created a table with "colspan" or "rowspan"?
ReplyDeleteMy IDE (VS2010) is having trouble recognizing the First() function after Paragraphs.
ReplyDeleteI have an insert but no inserttext or first. Anything I might be doing wrong?
How ca I insert table in specific position or insted of text
ReplyDeletehey 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
ReplyDeletewww.fgroupindonesia.com
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 :(
ReplyDeleteHi,
ReplyDeleteNice 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?
Cool simple library, but i'm missing the feature to AppendTable in a table/cell
ReplyDeleteHow to set vertical orientation for text in table?
ReplyDeleteI faced similar issue. Did you get any solution?
DeleteHow to add Numbered List inside Table Cell ?
ReplyDeleteNice article..
ReplyDeleteCan we check whether there is col span in table using docx package?
How transverse merged cell
ReplyDeleteHi, 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
ReplyDeletehowever 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.
Hi,
DeleteI have the same problem. Has the issue been solved?
Kind Regards
Hi! Did You find solution for this issue?
DeleteHow to add table in a numbered list. Is this library still maintained?
ReplyDeletehow I can add table at specific bookmark ?
ReplyDeleteDid you solved that problem? I have the same question!
DeleteSame!
DeleteSame
DeleteTry this:
Deletevar tableBookmark = document.Bookmarks.FirstOrDefault(b => b.Name == "tableBookmark");
var newTable = tableBookmark.Paragraph.InsertTableAfterSelf(5,5);
Hi, would it be possible to post a simple example that demonstrates creating a table of contents?
ReplyDeletethanks for the great component
Hi, would it be possible to post a simple example that demonstrates creating a table of contents?
ReplyDeletethanks for the great component
Hi Cathal,
ReplyDeleteI am using DocX for formatting an existing word document. It was working fine until I got a requirement of coloring bullet. It colors text of the bullet but I have to color the bullet itself. Is there any provision in your library to do this.
Thanks,
Atul
Hi, you are a life saver, i had been looking for something like this for a while
ReplyDeleteHow to add header on each page while printing table ?
ReplyDeleteHello. How do I protect a table cell so the user cannot edit it? Thank you.
ReplyDeleteHi, thanks for wonderful library. I am working on convert html to .docx. How would I set full width table in header?
ReplyDeleteHi, thanks for wonderful library. I am working on convert html to .docx. How would I set full width table in header?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteCan the table be positioned in a specific place on the page similar to the way ReplaceText works. I am building the table fine but it is not dropping into the template correctly.
ReplyDeleteHi,
ReplyDeleteCan I get the font family of a paragraph?
I tried:
var f = doc.Paragraphs[0].MagicText[0].formatting.FontFamily;
but I get : Name = error CS0103: the name 'name' does not exist in the current context, and I get back the default "Tahoma" - in all cases.
Thanks!
Naomi
Hi,
ReplyDeleteI would like to populate the table dynamically from a C# DataTable. Could you provide me an example.
Hello, thank you for you excellent library. I'am missing the function to vertically center text/grafic in an table cell. Is anybody close enough to the code to add this? thanks a lot in andvance
ReplyDeletereally great !!!!!!!!!!!!
ReplyDeletethank you soo much !!!!!!!!!!!!!!
Hi, how i can insert a table into a table's cell? Using this topTable.Rows[0].Cells[1].InsertTable(counterTable); i got an error opening the document. Thanks
ReplyDeleteI found the solution. After the insert table into the cell, add a paragraph like this topTable.Rows[0].Cells[1].InsertParagraph(document.InsertParagraph(""));
DeleteHello, I want to update TOC of word document after I processed on document and replaced data in template and saved as new document at different place. I am done with all other processing but how can I update the 'Table of Content' by using DocX library. Is there any auto update method exist for the same.
ReplyDeleteI have the same problem. Can you resolve it?
DeleteMe too. I have tha same problem
DeleteHi,
ReplyDeleteI have a table with 5 columns but table.InsertRow() keeps creating the row with only 3 cells. How can I prevent this?
Thanks.
This comment has been removed by the author.
DeleteHi,
ReplyDeleteI want to add a BackgroundColor to Cell, how can I do it? Can you tell me an example, please? Thanks.
It's quite easy to do:
DeleteNovacode.Table t = document.AddTable(3, 5);
t.Rows[0].Cells[0].FillColor = Color.LightGray;
Excellent, thanks a lot!!
DeleteHi, I have a problem with the width of the columns, when I insert text into the cell, change the width of the column, I used these codes, but the problem continues, is there other way?
ReplyDeletefloat[] colums = new float[] { 40, 150, 150, 300 };
tWhatsapp.SetWidths(colums);
tWhatsapp.Rows[i].Cells[2].Width = 150;
I've never got SetWidths to work correctly either. Here's how I set my column sizes by explicitly setting the column width to each cell in the table:
DeleteNovacode.Table t = document.AddTable(rows,columns);
t.AutoFit = Novacode.AutoFit.ColumnWidth;
for (int x = 0; x < t.ColumnCount; x++)
{
//t.SetColumnWidth(x, columnsizes[x]);
for (int y = 0; y < t.RowCount; y++)
{
t.Rows[y].Cells[x].Width = columnsizes[x];
}
}
Very good! Thank you for your help
DeleteHi the library is really helpful but I am stuck while adding a table inside a parent table cell . My documents as a nested table structure
ReplyDeletei am doing it as
maintable.Rows[0].Cells[0].InsertTable(childtbale).Alignment = Alignment.center;
but when I open the document it show the message "the document cannot be opened because there are problem with contents "
Thanks In advance
yes..even i am facing the same issue. how can we achieve table within a table using this lib.
DeleteHello, is there any way to set the font size for the whole table or for specific cells?
ReplyDeleteHere is some code I use to create a document table from a c# DataTable setting column sizes, header colour and font. You should be able to adapt this code for your own purposes
Deleteprivate Novacode.Table AddTableToWord(Novacode.DocX document, DataTable table, double[] columnsizes, double fontsize)
{
Novacode.Table t = document.InsertTable(table.Rows.Count + 1, table.Columns.Count);
// Set the column sizes
t.AutoFit = Novacode.AutoFit.ColumnWidth;
for (int x = 0; x < t.ColumnCount; x++)
{
//t.SetColumnWidth(x, columnsizes[x]);
for (int y = 0; y < t.RowCount; y++)
{
t.Rows[y].Cells[x].Width = columnsizes[x] * 37.8; // Centimeter to Word conversion
if (y > 0)
t.Rows[y].Cells[x].VerticalAlignment = Novacode.VerticalAlignment.Top;
else
t.Rows[y].Cells[x].VerticalAlignment = Novacode.VerticalAlignment.Center; // Header is centered
}
}
// Write the header
for (int x = 0; x < t.ColumnCount; x++)
{
t.Rows[0].Cells[x].Paragraphs.First().Append(table.Columns[x].ColumnName).Font(new FontFamily("Arial")).FontSize(fontsize).Color(Color.FromArgb(89, 119, 134)).Bold().Alignment = Novacode.Alignment.left;
t.Rows[0].Cells[x].FillColor = Color.LightGray;
}
int pos = 1;
foreach (DataRow r in table.Rows)
{
for (int x = 0; x < t.ColumnCount; x++)
{
t.Rows[pos].Cells[x].Paragraphs.First().Append(r[x].ToString()).Font(new FontFamily("Arial")).FontSize(fontsize).Color(Color.Black).Alignment = Novacode.Alignment.left;
}
pos++;
}
t.Rows[0].TableHeader = true;
return t;
}
FYI: I had to change the font part to: ....Font("Arial").....
ReplyDeleteI was originally running program in Visual Studio 2015, using DocX.1.0.0.22 - and no problems with ...FontFamily(new FontFamily("Arial"))....
I moved software to Visual Studio 2019, and am using the latest DocX (DocXCore.1.0.4). I got a string conversion error. Now, it works with just ...Font("Arial")...
Hey Guys use this online converter for file converting;
ReplyDeleteFile Spinner
That spinner will helps you to transfer you one file to other formats;
ReplyDeleteOdg to Pdf
Mkv to Mp3
Mp4 to Swf
Ogv to Zip
Docx to Zip
Hi there, You have done an excellent post on DOCX - HTML Converter. I’ll certainly digg it and personally recommend to my friends.
ReplyDeletePlay Emperor Casino Games Online | Shootercasino
ReplyDeleteExperience 제왕카지노 the feeling of Emperor Casino at Shootercasino. We 메리트카지노 provide online games like 카지노사이트 blackjack, baccarat, roulette, scratch cards and so much more!