Free Support Forum - aspose.com

InsertHtml() doesn't use font formatting of merge field

I’m trying to replace merge fields in a Word document with simple HTML formatted text using DocumentBuilder and MoveToMergeField() then InsertHtml() to each field.

The HTML text I’m inserting only has simple text formatting tags e.g.
etc.
What’s happening is that the merge field is replaced by the formatted text from the HTML but the formatting that was already applied to the field is lost. So any font colours or specific font size for the merge field is not respected.
How can I insert HTML formatted text into a merge field but keep the existing merge field paragraph properties?

This is currently what I am doing;
var builder = new DocumentBuilder(document);
builder.MoveToMergeField(“Title”);
builder.InsertHtml(“test test”);
Hi Andrew,

Thanks for your inquiry. I think, you can use the following code snippet to be able to merge the formatting that is specified inside HTML string with the formatting of MergeField:

Document doc = new Document(@"C:\test\mf.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToMergeField("mf", false, false);

InsertHtmlWithBuilderFormatting(builder, "plain html string");

//InsertHtmlWithBuilderFormatting(builder, "formatted html string");

builder.MoveToMergeField("mf");

doc.Save(@"C:\test\out.docx");


You can find code for InsertHtmlWithBuilderFormatting function in the attachment. I hope, this helps.

Best regards,

Hi Awais,

I've tested the code and it's doing exactly what I wanted, thank you very much for your help.

Regards

-Andrew

Hi,


Thanks for your feedback on this. It’s great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,