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

Free Support Forum - aspose.com

Mail Merge with html value

I have been using the mail merge feature to populate a template Word document. Works great. Now I have a field value that looks like html or xml. This example value has formatted text and a picture (stream truncated).



This is a picture of a park bench


<img alt="" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAAAAAAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQN



I cannot seem to figure out how to merge this into the Word document and have it come out as formatted text and a picture. It is more than an image and only this field is html while the other fields being merge are plain text.
Hi Steve,

Thanks for your inquiry. Please see the following code. In this way you can insert html at merge field's location.
Document doc = new Document(getMyDir() + "in.docx");

doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(
new String[] { “html” },
new String[] {

});

doc.save(getMyDir() + “15.9.0.docx”);

static class HandleMergeField implements IFieldMergingCallback
{
public void fieldMerging(FieldMergingArgs e) throws Exception
{
if (e.getFieldName().equals(“html”)) {
Document doc = (Document) e.getDocument();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToMergeField(e.getFieldName());
builder.insertHtml(e.getFieldValue().toString());
}
}
<font color="RED"><b>public</b></font> <font color="RED"><b>void</b></font> imageFieldMerging<font color="BLUE"><b>(</b></font>ImageFieldMergingArgs args<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception
<font color="BLUE"><b>{</b></font>
    <font color="GREEN"><i>// Do nothing.

}
}

Best regards,

Thank you. That worked great.

Any chance of adding this to the programmer examples documentation?

Hi Steve,


Thanks for your request. We will consider adding a separate article for this case. However, this concept is well documented in the following articles:

How to Insert Check Boxes during Mail Merge
How to Apply Custom Formatting during Mail Merge

Best regards,