We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

API to Create Table Styles in Word Document Programmatically

Hello,

I am using Aspose.Words 10.5 to create Word Documents. However, when I attempt to create a new Table Style, I receive an error stating “Cannot create table styles. Parameter name: type”, with no other information.

I’m attempting to create a table style using the following code:

style = documentNode.Styles.Add(StyleType.Table, "t" + domStyle.Id.Value);

style is an Aspose.Words.Style object,
_documentNode is an Aspose.Words.Document object.
domStyle.Id.Value is a string.

I am able to create Paragraph and Character styles just fine, but not Tables. The constructor looks like it’s just the enum StyleType and a string. Could you tell me what I’m doing incorrectly?

Hi David,


Thanks for your inquiry.

I’m afraid creating new tables is unsupported. Currently you can use any of the in-built table styles and any table style defined within the template.

I have linked your request to the appropriate issue. We will inform you as soon as there are any developments.

Thanks,

The issues you have found earlier (filed as WORDSNET-5312) have been fixed in this Aspose.Words for .NET 19.6 update and this Aspose.Words for Java 19.6 update.

@techinmd,

Please use the following code of Aspose.Words for .NET API to be able to programmatically create a new Table style in MS Word documents:

Add Table Style in DOCX file - use TableStyle Class to specify Settings for Table

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Name");
builder.InsertCell();
builder.Write("مرحبًا");
builder.EndRow();
builder.InsertCell();
builder.InsertCell();
builder.EndTable();

TableStyle tableStyle = (TableStyle)doc.Styles.Add(StyleType.Table, "MyTableStyle1");
tableStyle.AllowBreakAcrossPages = true;
tableStyle.Bidi = true;
tableStyle.CellSpacing = 5.0;
tableStyle.BottomPadding = 20.0;
tableStyle.LeftPadding = 5;
tableStyle.RightPadding = 10;
tableStyle.TopPadding = 20.0;
tableStyle.Shading.BackgroundPatternColor = Color.AntiqueWhite;
tableStyle.Borders.Color = Color.Black;
tableStyle.Borders.LineStyle = LineStyle.DotDash;

table.Style = tableStyle;

// Some Table attributes are linked to style variables
Assert.AreEqual(true, table.Bidi);
Assert.AreEqual(5.0, table.CellSpacing);
Assert.AreEqual("MyTableStyle1", table.StyleName);

doc.Save("E:\\Temp\\19.12.docx");

The above C# code will add a formatted Table in Word document as shown in following screenshot: