Builder's Table not applying HTML styles

HTML Style inserted into a Words Document Builder does not get applied through to tables. If I were to just simply InsertHtml all of the text outside of the table, the formatting would be correctly shown. I know the InsertHtml for tables works because I have inserted bold content just fine, its just anything that is inserted that has a style to inherit from the page does not get applied.

C# Code

Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

string styleHeader = ".ins{background-color:#cfc;text-decoration:inherit;}";

builder.InsertHtml(styleHeader);

String documentVersion = "1.0";

Aspose.Words.Tables.Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Version:");
builder.InsertCell();
builder.InsertHtml(documentVersion);
builder.EndRow();
builder.EndTable();

// table.StyleIdentifier = StyleIdentifier.LightShadingAccent1; //Optional Formatting

HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);

options.CssStyleSheetType = CssStyleSheetType.Embedded;

builder.Document.Save(HttpContext.Current.Response, "Test.doc", Aspose.Words.ContentDisposition.Attachment, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(SaveFormat.Doc));

Hi Philip,

Thanks for your inquiry. Please note that the content inserted by DocumentBuilder.InsertHtml** method does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. If you insert HTML with no formatting specified, then default formatting is used for inserted content.

I suggest you please use InsertHtmlWithBuilderFormatting instead of the InsertHtml method as shown in following code snippet. I have attached the code related to InsertHtmlWithBuilderFormatting with this post.

Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
string styleHeader = ".ins{background-color:#cfc;text-decoration:inherit;}";
builder.InsertHtml(styleHeader);
String documentVersion = "1.0";
Aspose.Words.Tables.Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Version:");
builder.InsertCell();
// builder.InsertHtml(documentVersion);
Style style = doc.Styles["ins"];
builder.ParagraphFormat.Style = style;
InsertHtmlWithBuilderFormatting(builder, documentVersion);
builder.EndRow();
builder.EndTable();
builder.ParagraphFormat.ClearFormatting();
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
options.CssStyleSheetType = CssStyleSheetType.Embedded;
builder.Document.Save(MyDir + "Out.doc", options);

Followup question, where is InsertHtmlWithBuilderFormatting? I went around and googled after not finding it on the builder or document objects, found the post https://forum.aspose.com/t/53489 which includes the method as a hack.

Second question, if I’m converting an entire HTML document to Word using Aspose and it references a stylesheet, how is that handled differently than this scenario? Is it because one is all or nothing, the other is piece meal?

Hi Philip,

Thanks for your inquiry.

*philip.betts:

Followup question, where is InsertHtmlWithBuilderFormatting? I went around and googled after not finding it on the builder or document objects, found the post Problem with (RTL-LTR) mixed report which includes the method as a hack.*

I attached the code of InsertHtmlWithBuilderFormatting in my previous post here. Perhaps you missed that attachment.

*philip.betts:

Second question, if I’m converting an entire HTML document to Word using Aspose and it references a stylesheet, how is that handled differently than this scenario? Is it because one is all or nothing, the other is piece meal?*

The content inserted by DocumentBuilder.InsertHtml method does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. You can use InsertHtml method to insert an HTML fragment or whole HTML document. In this case, you do not need to use InsertHtmlWithBuilderFormatting.

Moreover, please note that Aspose.Words mimics the same behavior as MS Word do. Upon
processing HTML, some features of HTML might be lost. You can find a
list of limitations upon HTML exporting/importing here:
https://docs.aspose.com/words/net/load-in-the-html-html-xhtml-mhtml-format/
https://docs.aspose.com/words/net/save-in-html-xhtml-mhtml-formats/