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 Jan 02, 2020 3:50 am

I want to apply a style to a table ,with it's cells and rows,my style Name is "myCustomStyle",how to apply this style to my table?

standheo
 
Posts: 24
Joined: Mon Oct 08, 2018 11:13 am

Thu Jan 02, 2020 6:41 am

Hi,

Thanks for your inquiry.
What is your custom style? Is it ParagraphStyle? If so, please refer to following code:
Code: Select all
            Document doc = new Document(filePath + @"20080.docx");
            Section sec = doc.Sections[0];
            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "MyCustomStyle";
            style.CharacterFormat.FontSize = 20;
            style.CharacterFormat.Bold= true;
            doc.Styles.Add(style);
           
            Table table = sec.Tables[0] as Table;
            foreach(TableRow row in table.Rows )
            {
                foreach(TableCell cell in row.Cells)
                {
                    foreach(Paragraph par in cell.Paragraphs)
                    {
                        par.ApplyStyle("MyCustomStyle");
                    }
                }
            }
            doc.SaveToFile("T20080.docx");

If not, please share detailed information along with the input file(if any) and the code you used.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Jan 03, 2020 2:25 am

no,it's a tableStyle,in ms-word,it like the addachment picture,can spire.doc support this "new table style" operation?

standheo
 
Posts: 24
Joined: Mon Oct 08, 2018 11:13 am

Fri Jan 03, 2020 3:34 am

Hi,

Thanks for your information.
Sorry that Spire.Doc doesn't support that at present. We will consider adding the new feature in our future upgrade. Once there is any update, we will let you know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Sep 30, 2025 6:32 am

Hi,

Thanks for your patient waiting.
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

Return to Spire.Doc