Formatting images in Aspose.Words java MailMerge

Hi,
I am using aspose to build a custom document and need some help with Mail merge.
I am using the doc.getMailMerge().addMergeImageFieldEventHandler interface to build the image in the document.
I was able to add images into mail merge but I have a few questions:

  1. How do I format(shrink or expand) image size using mailMerge. I cannot use document builder beacuse the number of images that are displayed in the document are not fixed(Dynamic).
  2. I am using the doc.getMailMerge().addMergeImageFieldEventHandler interface. I have 2 records and 2 images so mail merge should display image1 with record1 and image2 with record2. But the Image1 is getting repeated for record 1 and record 2 as well. (In short only one image is getting repeated for multiple reocords). Can you provide some example for doing this the right way.

Thanks,
Saurabh

This message was posted using Aspose.Live 2 Forum

Hi Saurabh,
Thanks for your inquiry.
I think you can use code like this below. It will show you how to resize an image before it is merged and also a simple way how to handle multiple images within the image handler.

public static class HandleMergeImages implements IFieldMergingCallback
{
    public void fieldMerging(FieldMergingArgs args)
    {}
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
    {
        // Construct the image path from the supplied prefix, the current record and the image extension.
        String imagePath = (String) args.getFieldValue() + args.getRecordIndex() + ".jpg";
        // Load the reference image
        BufferedImage image = ImageIO.*read * (new File(imagePath));
        // Resize the image based off the original size and set it to be merged.
        args.setImage(resizeImage(image, (int)(image.getWidth() * 0.5), (int)(image.getHeight() * 0.5)));
    }
    public BufferedImage resizeImage(BufferedImage image, int width, int height)
    {
        BufferedImage bufferedImage = new BufferedImage(width, height, image.getType());
        Graphics2D g = bufferedImage.createGraphics();
        Image resizedImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        g.drawImage(resizedImage, null, null);
        return bufferedImage;
    }
}

If you have any queries, please feel free to ask.
Thanks,

Hi Adam,

Thanks for the quick reply. I was trying to implement the solution that you provided but I am running into trouble with few tings. Maybe I was not clear in my query. I am using the trial instance of Aspose.Words for java. Please help me locate the following classes
IFieldMergingCallback
FieldMergingArgs
ImageFieldMergingArgs
Graphics2D

I tried to get these classes from Aspose but they are all pointing towards .Net
Do you have any solution that would work with java?
I would love to hear back from you, thank you for your time and patience.

Sincerely,
Saurabh

Hi Saurabh,
Thanks for this additional information.
In the latest versions of Aspose.Words for Java the field merging interface has been renamed to be in sync with the .NET platform.
Please find the code below which is the same implementation in the old API. Also note that Graphics2D is a standard Java library and can be found under java.awt.Graphics2D.

public static class HandleMergeImageField implements MergeImageFieldEventHandler
{
    public void mergeImageField(Object sender, MergeImageFieldEventArgs e)
    {
        // Construct the image path from the supplied prefix, the current record and the image extension.
        String imagePath = (String) e.getFieldValue() + e.getRecordIndex() + ".jpg";
        // Load the reference image
        BufferedImage image = ImageIO.*read * (new File(imagePath));
        // Resize the image based off the original size and set it to be merged.
        e.setImage(resizeImage(image, (int)(image.getWidth() * 0.5), (int)(image.getHeight() * 0.5)));
    }
}

Thanks,

Hi,

Thanks for the information it was very helpful.
I have another query, how do we hide the aspose fields from the word document if they are not populated? I have created a bunch of fields for the images on my template but all the fields are not getting populated and I wish to hide those fields on the generated word document which remain empty.

Thanks,
Saurabh

Hello

Thanks for your request. I think you can try specifying MailMerge.RemoveEmptyParagraphs option; in this case all empty paragraphs will be removed during mail merge process.
Please note, if the paragraph contains at least one character (like white space) this paragraph will not be removed.
Best regards,

Actually I am adding Image:ImageName for adding images using mail merge. I am adding 10 image fields on my template but if I receive only 5 images I would love to hide the rest of the 5 image fields. I am not using paragraph anywhere so is there any other way to hide mergefields directly?

Thanks,
Saurabh

Hi Saurabh,
Thanks for your request. You can just call MailMerge.DeleteFields method after executing mail merge. In this case all non-filled fields will be removed from the document.
Best regards,