Aspose mail merge image on image

1.4k Views Asked by At

Is there any way to insert image on image using mustache syntax or in java creating docx report?

In docx I declare background image like this: {{ Image:BackgroundImage }}. And I'am running this code.

Document doc = new Document(dataDir + "ImagesTemplate.docx");

doc.getMailMerge().execute(new String[]{"BackgroundImage","AnpotherImage"}, new Object[]{"image1.png","image2.png");

    doc.save(dataDir + "ImageOutput.docx");

Of course paths to images are not constant. Is there's a way to insert image2 on to image1?

1

There are 1 best solutions below

0
On

There is no such method in the Aspose.Words API to render image on another image. But, you can achieve this using custom code.

In your example, you passed both the background and foreground images, this means there will be two separate field. We need to pass just one field. In the custom code, we can put 2 or more images in it.

// Suppose the field name is just "Image"
doc.getMailMerge().setFieldMergingCallback(new HandleMergeFields()); // This method will do the trick
doc.getMailMerge().execute(new String[]{"Image"}, new Object[]{Common.DATA_DIR + "images/001-front.jpg"});

Field name in the Word template should also be "Image".

About the file name, I assumed that you pass only one file name e.g. "001-front.jpg". In the custom code, I will extract the background image using it. The corresponding image will be "001-back.jpg"

Now the custom override method for handling image.

private static class HandleMergeFields implements IFieldMergingCallback 
{
    public void fieldMerging(FieldMergingArgs e) throws Exception 
    {
        if (mBuilder == null)
            mBuilder = new DocumentBuilder(e.getDocument());

        // If the field name is "Image"
        if (e.getFieldName().equals("Image")) 
        {
            try
            {
                // Go to that place and remove the field
                mBuilder.moveToMergeField("Image", true, true);

                // Get the field value
                String fieldValue = (String)e.getFieldValue();
                // Field value is the front image
                String frontImgPath = fieldValue;
                // Get the background image.
                // You can use any logic here to find your background image
                String backImagePath = frontImgPath.replace("-front.jpg", "-back.jpg");

                // First insert the background image in the document
                BufferedImage backImage = ImageIO.read(new File(backImagePath));
                Shape shape = mBuilder.insertImage(backImage);
                shape.setWrapType(WrapType.NONE);

                // Now insert the foreground image
                File secondFile = new File(frontImgPath);
                if (secondFile.exists() == true)
                {
                    BufferedImage imgsecond = ImageIO.read(secondFile);

                    Shape shapeLegend = mBuilder.insertImage(imgsecond);
                    shapeLegend.setWrapType(WrapType.NONE);
                    // Position where to add the foreground image
                    double secondTop = 0;
                    double secondLeft = shape.getWidth() - shapeLegend.getWidth();
                    shapeLegend.setTop(secondTop);
                    shapeLegend.setLeft(secondLeft);
                }
            }
            catch(Exception ex)
            {
                System.err.println("Can't load map image. Exception: " + ex.getMessage());
            }
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception 
    {
        try
        {
        }
        catch(Exception ex)
        {
            System.err.println("Error putting map image. Exception: " + ex.getMessage());
        }
    }

    private DocumentBuilder mBuilder;
}