Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Dec 05, 2019 1:01 pm

Hello everybody,

i tried to insert a table into a docx-Document by searching for a Bookmark and inserting my Table there:
Code: Select all
...open Document...
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
bookmarkNavigator.MoveToBookmark(bookmarkName);
bookmarkNavigator.InsertTable(table);
...save Document...

That works fine.

Now i tried to set the Style of my new Table to a custom Style in our given Document. I can find the Style by,
Code: Select all
Spire.Doc.Documents.Style style = doc.Styles.FindByName("CustomStyleName");

but i have no Idea how to set the Style to my table.
I just can set the Style to a DefaultTableStyle from this enum by this:
Code: Select all
table.ApplyStyle(DefaultTableStyle...);

but i want to set the Style to a custom one.

many thanks and best regards,
Joachim

joachim_eckerl
 
Posts: 10
Joined: Thu Dec 05, 2019 12:52 pm

Fri Dec 06, 2019 3:36 am

Hello,

Thanks for your inquiry.
Sorry for the turth that our Spire.Doc doesn't support applying a custom table style for table at present. We will consider adding this new feature into our further upgrade, if it is achieved in the future, I will inform you.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Fri Dec 06, 2019 8:14 am

Hello,

thanks for your answer.
Now i can stop searching.

Best regards,
Joachim

joachim_eckerl
 
Posts: 10
Joined: Thu Dec 05, 2019 12:52 pm

Mon Sep 29, 2025 9:18 am

Hi,

Glad to inform you that we released the Spire.Doc Pack(hot fix) Version:13.9.10, which provides the table.Format.Style and ApplyStyle(ITableStyle) to apply styles to tables. Please download it from the following link.
Website link: https://www.e-iceblue.com/Download/download-word-for-net-now.html
NuGet link:https://www.nuget.org/packages/Spire.Doc/13.9.10
https://www.nuget.org/packages/Spire.Docfor.NETStandard/13.9.10
Code: Select all
Document doc = new Document();
Section section = doc.AddSection();

TableStyle tableStyle = (TableStyle)doc.Styles.Add(StyleType.TableStyle, "TestTableStyle1");
tableStyle.HorizontalAlignment = RowAlignment.Center;
tableStyle.Borders.Color = Color.Blue;
tableStyle.Borders.BorderType = BorderStyle.Single;

Table table = section.AddTable();
table.ResetCells(1, 1);
table.Rows[0].Cells[0].AddParagraph().AppendText("Aligned to the center of the page");
table.PreferredWidth = PreferredWidth.FromPoints(300);

table.ApplyStyle(tableStyle);

section.AddParagraph().AppendText(" ");

tableStyle = (TableStyle)doc.Styles.Add(StyleType.TableStyle, "TestTableStyle2");
tableStyle.LeftIndent = 55;
tableStyle.Borders.Color = Color.Green;
tableStyle.Borders.BorderType = BorderStyle.Single;

table = section.AddTable();
table.ResetCells(1, 1);
table.Rows[0].Cells[0].AddParagraph().AppendText("Aligned according to left indent");
table.PreferredWidth = PreferredWidth.FromPoints(300);

table.Format.Style = tableStyle;

doc.SaveToFile(outputDocxFile1, FileFormat.Docx);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Fri Nov 21, 2025 1:04 pm

I need to generate the table with attached format could you share the sample code for generating the table and add font size and name and style using spire.doc .net version 13.10.3

eswaribaskar
 
Posts: 5
Joined: Fri Nov 21, 2025 5:30 am

Mon Nov 24, 2025 9:45 am

Hello eswaribaskar,

Thank you for your letter.
Please refer to the following example to create a table. If you have any other questions, please feel free to contact.
Code: Select all
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph title = section.AddParagraph();
            title.AppendText("test");
            title.Format.HorizontalAlignment = HorizontalAlignment.Center;
            title.Format.AfterSpacing = 10f;

            //add table Paragraph style
            string pStyleName = "testStyle";
            ParagraphStyle pStyle = (ParagraphStyle)doc.Styles.Add(StyleType.ParagraphStyle, pStyleName);

            if (pStyle is ParagraphStyle)
            {
                ParagraphStyle ps = pStyle as ParagraphStyle;
                ps.CharacterFormat.FontName = "Arial";
                ps.CharacterFormat.FontSize = 14;
                ps.CharacterFormat.Bold = true;
                ps.CharacterFormat.TextColor = Color.Black;

            }

            //add Table
            Table table = section.AddTable(true);
            table.ResetCells(3, 4);

            table.Format.LayoutType=LayoutType.Fixed;
            table.PreferredWidth = new PreferredWidth(WidthType.Percentage,(short) 100);

            //table data
            table.Rows[0].Cells[0].AddParagraph().AppendText("A");
            table.Rows[0].Cells[1].AddParagraph().AppendText("B");
            table.Rows[0].Cells[2].AddParagraph().AppendText("C");
            table.Rows[0].Cells[3].AddParagraph().AppendText("D");
            table.Rows[1].Cells[0].AddParagraph().AppendText("1");
            table.Rows[1].Cells[1].AddParagraph().AppendText("2");
            table.Rows[1].Cells[2].AddParagraph().AppendText("3");
            table.Rows[1].Cells[3].AddParagraph().AppendText("4");
            table.Rows[2].Cells[0].AddParagraph().AppendText("a");
            table.Rows[2].Cells[1].AddParagraph().AppendText("b");
            table.Rows[2].Cells[2].AddParagraph().AppendText("c");
            table.Rows[2].Cells[3].AddParagraph().AppendText("d");

            // set border
            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    cell.CellFormat.Borders.Left.BorderType = BorderStyle.None;
                    cell.CellFormat.Borders.Left.LineWidth = 0f;
                    cell.CellFormat.Borders.Right.BorderType = BorderStyle.None;
                    cell.CellFormat.Borders.Right.LineWidth = 0f;
                    cell.CellFormat.Borders.Top.BorderType = BorderStyle.Single;
                    cell.CellFormat.Borders.Top.LineWidth = 0.5f;
                    cell.CellFormat.Borders.Bottom.BorderType = BorderStyle.Single;
                    cell.CellFormat.Borders.Bottom.LineWidth = 0.5f;
                    for (int i = 0; i < cell.Paragraphs.Count;i++)
                    {
                        cell.Paragraphs[i].ApplyStyle(pStyleName);
                    }
                   
                }
            }

            // save
            doc.SaveToFile("table.docx", FileFormat.Docx2013);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Mon Nov 24, 2025 10:53 am

Hi Team,

Thanks for your support !

This is my final updated code. Everything is working properly except the Table of Contents. The heading numbers in the TOC are not updating correctly. In the document, the correct heading numbers are generated, but the Table of Contents is not reflecting the correct values.
I have attached my final version of the code and the Word output.
Kindly review and provide a solution to fix this issue.

eswaribaskar
 
Posts: 5
Joined: Fri Nov 21, 2025 5:30 am

Tue Nov 25, 2025 7:58 am

Hello eswaribaskar,

Thank you for your feedback.
Yes, I did find that the list number was not updated correctly in the TOC. I have logged this issue into our bug tracking system with SPIREDOC-11692, Our Dev team will do more investigations to resolve it.
At the same time, if I reload the current result and update the TOC, the output can be updated correctly. You can use this temporary solution first, once SPIREDOC-11692 is fixed, we will provide the hotfix version for you.
Code: Select all
       Spire.Doc.Document documentNew = new Spire.Doc.Document();
       documentNew.LoadFromFile(@"ProjectMonthlyReport.docx");
       documentNew.IsUpdateFields = true;
       documentNew.UpdateTableOfContents();
       documentNew.SaveToFile(@"ProjectMonthlyReport-out.docx", Spire.Doc.FileFormat.Docx);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Thu Jan 15, 2026 9:06 am

Hello eswaribaskar,

Regarding the issue of SPIREDOC-11692, please use the adjusted code in the attachment for testing. If you have any other questions, just feel free to write back.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc