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

Free Support Forum - aspose.com

Replace the image after performing mail merge using Java


So we have an application that does mail merge to merge text. The document we produce can change all the time, so we have implemented it so that it keeps the merge fields after mail merge, and remerges the document later. This is the code we use:

public void fieldMerging(FieldMergingArgs fieldMergingArgs) throws Exception {
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToField(fieldMergingArgs.getField(), true);
builder.insertField(fieldMergingArgs.getField().getFieldCode(), fieldMergingArgs.getFieldValue().toString());

A new requirement is to also merge images. I have successfully merged images using the following code:

public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception {

imageFieldMergingArgs.setImageStream(new ByteArrayInputStream(image));


However, we would also want to remerge(mail merge) the image after having already merged it. Can you show me how? :slight_smile:



To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

I attached three documents. No.1 is a sample input file with an image mergefield. No.2 is visually the expected output after merge; but not including the mergefield. I don’t know how that should look?

No.3 is visually the expected output after the second merge; but again, not including the mergefield.

So what I want is that after the first merge, the image is merged into the document. After the second merge, the already merged image is replaced by the second image.

docs.zip (146.3 KB)


In your case, we suggest you following solution.

  1. Please implement IFieldMergingCallback interface.
  2. In IFieldMergingCallback.ImageFieldMerging, move the cursor to the mail merge field and insert the image using DocumentBuilder.InsertImage. This method returns Shape node object.
  3. After inserting the image, please use Shape.AlternativeText property to set the image’s alternative text.
  4. To replace the image, you need to get the image using alternative text from the document.
  5. Move the cursor to this image and insert the new image.
  6. Remove the old image using Shape.Remove method.

We suggest you please read the following articles. Hope this helps you.

Use DocumentBuilder to Insert Document Elements
Using DocumentBuilder to Modify a Document
Setting Image Properties during Mail Merge

Ok, I’ll try this. Thank you :slight_smile: