How to insert HTML into Word document with DocumetBuilder formatting using .NET | InsertHTML

I am doing a proof of concept and am using aspose word to populate a word document bookmark with html codes for the font styles such as bold and italic. The bookmark is formated with a size and color inside of the word document. If i use

bookmark = document.Range.Bookmarks("MyBookmark")
bookmark.Text = "My Text"

the size and color stay, but obviously the HTML is part of the text.

When I use

bookmark = document.Range.Bookmarks("MyBookmark")
builder.MoveToBookmark(bookmark.Name)
builder.InsertHtml("My Text")

The text is correctly bolded but the formating that was set in the word document is now lost. Is there a way to insertHtml and still keep the syle set in the work document for the bookmark?

Hi Brad,


Thanks for considering Aspose.

I’m afraid when using InsertHtml, all formatting is expected to come from the HTML content, and not from the builder formatting.

To achieve a combination of formatting from both sources you can look into using the work around code from here: https://forum.aspose.com/t/53489 The class is a attachment on that post.

If we can help with anything else, please feel free to ask.

Thanks,

Hi...I am having a similar issue but kind of in reverse. When I use builder.InsertHtml, the text that is inserted is all bolded even though there is no bolding tag in the html. Any help would be Greatly appreciated. This is a show stopper for our project

Thanks in advance

Mike

Hi Mike,


Thanks for your inquiry.

Could you please attach your document, HTML input and code here for testing? We will take a look into this issue for you.

Thanks,

Hi Adam...thank you.

It appears that the formatting is defaulting to the "Normal" Word template style. The font that is getting passed into my InsertHtml() method is not being applied. The font produced in the document is "Times New Roman"/12 but the font being passed in the font paramater is Arial size10. The text in Description and the Risks table have Times New Roman instead of Arial. I hope we can figure this out.

Attached is my code. and the resulting document

Hi Mike,


Thanks for attaching your document and code here.

The issue that you are experiencing is actually the same as the original issue in this thread. The formatting that you specify through the builder formatting (bold, size, name) is not applied to the HTML content inserted using InsertHtml. In recent versions the formatting from HTML is expected to be fully specified on the HTML itself and not the builder formatting.

To get a combination of both formatting of the HTML and the builder you can use the InsertHtmlWithBuilderFormatting code which is linked above. Just use that code and replace the call to builder.InsertHtml(html) with InsertHtmlWithBuilderFormatting(builder, html) in your existing code. This should solve the issue.

Thanks,

Hi Adam.....that's terrific!

Thanks for the response. I came up with a solution, too. I leveraged another thread on the Aspose support site and used some ideas from there. Here's the url: Word Html Formatting issue

I have attached my code that is now working. I am going to download your InsertHtmlWithBuilderFormatting code, as well, and educate myself on how it works.

Thanks again for your response. I will continue to Post here when I need support. Good stuff.

Cheers

Mike

Hi Mike,


Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.