We created a merge field in both the header and the body of a word document with the same name. After running InsertHtml, it appears to have inserted itself in the header once as plain text, and twice in the body as the expected converted HTML. When the file only has one merge field in the header, the output correctly converts the HTML in the field for the header. I’ve attached the input file (rich in header (1).docx) and the output file (rich in header_out.docx).
Code
var inputStream = System.IO.File.OpenRead(@"C:\Users\Dan\Downloads\rich in header (1).docx");
var document = new Aspose.Words.Document(inputStream);
document.MailMerge.FieldMergingCallback = new HandleMergeField();
document.MailMerge.Execute(new string[] {
"Title:RichText"
}, new object[] {
"<p>Developer</p>"
});
Merge Field Handler
class HandleMergeField : IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
var builder = new DocumentBuilder(e.Document);
if (e.FieldName.ToLower().Contains(":richtext"))
{
builder.MoveToMergeField(e.FieldName);
builder.InsertHtml((string)e.FieldValue);
return;
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
}
}