Images not appearing in Mail Merge

We are using mail merge and would like to display images dynamically. This is using Aspose.Words with Java.We have tried this several different ways and nothing is working.

Our handler:

private class HandleMergeRequestImage implements MergeImageFieldEventHandler
{
public void mergeImageField(Object sender, MergeImageFieldEventArgs e)
{
java.awt.image.BufferedImage image = e.getImage();

if (e.getFieldValue() != null && !"".equals(e.getFieldValue()))
e.setImageFileName((String)e.getFieldValue());
}
}

This method loads the correct fields:

private void addQuotedRequestedValuesField(Map<String, Object> fields, String field, String quotedValue, String requestedValue) throws Exception {
fields.put("QV\_"+field, quotedValue);
fields.put("RV\_"+field, requestedValue);

if (!requestedValue.equals(quotedValue))
fields.put("Image:"+field, "http://localhost:8080/image/icons/edit1.gif");
else
fields.put("Image:"+field, "");
}

So, for example, we might call the addQuotedRequestedValuesField method this way:
this.addQuotedRequestedValuesField(fields, “MCFP”, code, requestedValue);

later on, we call a mail merge like this:

// Perform Mail Merge
// ==================
String[] tags = new String[fields.size()];
Object[] values = new String[fields.size()];

fields.keySet().toArray(tags);
fields.values().toArray(values);

// Throws: Exception
document.getMailMerge().execute(tags, values);

When debugging the image handler, getFieldValue() is always null. Why?
Thanks,
Kim

Hi

Thanks for your inquiry. It seems the problem occurs because you put “Image:fieldName” value into the field names array, but you should put only “fieldName” without “Image”.

Also if you store full path to image in your data source then you don’t need mergeImageField event handler at all. See the following code:

//Create names and values arrays
String[] names = { "myImg" };
Object[] values = { "http://www.aspose.com/Images/aspose-logo.jpg" };
//Open template
Document doc = new Document("in.doc");
//Execute mail merge
doc.getMailMerge().execute(names, values);
//Save output doc
doc.save("out.doc");

In the template I have only one mergefield with name “Image:myImg”.

Also see the following link to learn more about using MergeImageFiled event handler.

Best regards.

Thank you! This worked. There are 2 developers here kicking ourselves now over how easy that was… and wondering how we missed that in the documentation.