Insert Image with specific height - Aspose Words for Java

Hello and good day,


I have a small application that uses a template Word document and replaces certain words in it with other words and an image using the getMailMerge() function. Part of this process includes inserting an image. (See source guide below)

I would like to ask how I can insert the image in a specific height dimension, because images too large would ruin the layout of my document. Any sample code snippet would be much appreciated.

SOURCE GUIDE:Types of Mail Merge Operation in Java|Aspose.Words for Java

Regards,

Hi Junmil,

Thanks for your inquiry. Please try running the following simple code to be able to insert images during performing mail merge operation.
Document doc = new Document(getMyDir() + "in.docx");

doc.getMailMerge().setFieldMergingCallback(new HandleMergeFields());
doc.getMailMerge().execute(new String[]{"pic"}, new Object[]{getMyDir() + "Aspose.Words.jpg"});

doc.save(getMyDir() + "15.11.0.docx");
static class HandleMergeFields implements IFieldMergingCallback
{
public void fieldMerging(FieldMergingArgs args) throws Exception {
if (args.getFieldName().equals("pic") && args.getFieldValue() != null)
{
DocumentBuilder builder = new DocumentBuilder(args.getDocument());
builder.moveToMergeField(args.getFieldName());
Shape img = builder.insertImage(args.getFieldValue().toString());
img.setWidth(300);
img.setHeight(300);
}
}

public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception {
// Do nothing.
}
}
Best regards,

Hello Awais,


Thanks for the code snippet, Im currently trying it out, but is there a way to be able to set the aspect-ratio to scale?

I just need to adjust the height of the image, and want the width to scale accordingly, since the images can be any aspect ratio but must fit a certain portion of the document. (square, vertical rectangle, horizontal rectangle, etc)

Hi Junmil,


Thanks for your inquiry. We have logged your requirement in our issue tracking system as WORDSNET-12866. Our product team will further look into the details of this problem and we will keep you updated on the status of this issue. We apologize for any inconvenience.

Best regards,

So does this mean that Aspose Words does not currently support only setting 1 dimension (height/width) of an image and have the other dimension (width/height) scale accordingly?

Hi Junmil,


Thanks for your inquiry. You can set one dimension but it will not have any effect on the other dimension. Yes. your understanding is correct that currently you can’t programmatically set the Aspect Ratio of image to Scale. We will inform you as soon as this feature is supported. We apologize for any inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-12866) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.