How to set default table style?

Hello,

How do we set the default style for all the tables?

Thanks!

@gojanpaolo,

Please try using the following code:

Document doc = new Document("E:\\temp\\input.docx");

foreach(Table tab in doc.GetChildNodes(NodeType.Table, true))
{
    tab.StyleName = "Grid Table 5 Dark";
}

doc.Save("E:\\temp\\19.1.docx");

Hope, this helps.

@awais.hafeez

Thanks for the suggestion but we usually create a document using the DocumentBuilder. Please see below code which demonstrates our use case.

var documentBuilder = new DocumentBuilder();

// TODO : set default style for new table
// e.g. table borders and shading, font, etc

// from here on, all new tables should have default style

documentBuilder.StartTable();
documentBuilder.InsertCell();
documentBuilder.Write("test");
documentBuilder.EndRow();
var table = documentBuilder.EndTable();

@gojanpaolo,

Please check the following code:

Document doc = new Document();
var documentBuilder = new DocumentBuilder(doc);

documentBuilder.StartTable();
documentBuilder.InsertCell();
documentBuilder.Write("test");
documentBuilder.InsertCell();
documentBuilder.Write("test");
documentBuilder.EndRow();
documentBuilder.InsertCell();
documentBuilder.Write("another row");
documentBuilder.InsertCell();
documentBuilder.Write("test");
documentBuilder.EndRow();
var table = documentBuilder.EndTable();

// Assign appropriate table style after when you finish the construction of Table
table.StyleName = "Grid Table 5 Dark";

doc.Save("E:\\temp\\19.1.docx");