Free Support Forum - aspose.com

Image Field Merging - Dimensions not working?

Hi,


I’m trying to set the width and/or height of images which are beeing inserted as a mailmerge field. In the Word Template if I insert a merge field with the name “Image:MyImageField”, it’s working fine as expected. When I set it like “Image(100;100):MyImageField” or “Image(20pt;50pt):MyImageField” I get exactly the same result.

So I’m not sure what is wrong?

My other option was to try it with an IFieldMergingCallback, but it is not working as expected for PHP JavaBridge:

If I use the following code PHP is throwing me an error that IFieldMergingCallback is not an interface (because of “implements”):

[…]

$this->doc->getMailMerge()->setFieldMergingCallback(new HandleMergeField());

[…]

class HandleMergeField implements IFieldMergingCallback {

public function fieldMerging(FieldMergingArgs $args)
{
}

public function imageFieldMerging(ImageFieldMergingArgs $args)
{
echo java_values($args->ImageWidth->Value);
}

}

And if I use the following, nothing at all happens (with “extends”):


[…]

$this->doc->getMailMerge()->setFieldMergingCallback(new HandleMergeField());

[…]

class HandleMergeField extends IFieldMergingCallback {


function __construct()
{
echo “Hello World”; //this is happening
}

public function fieldMerging(FieldMergingArgs $args)
{
}

public function imageFieldMerging(ImageFieldMergingArgs $args)
{
echo java_values($args->ImageWidth->Value);
}

}

Thanks for any suggestions!

Hi there,


Thanks for your inquiry. Please note in simple Java you can resize image in MailMerge process as following. We will appreciate it if you please share your sample PHP working code here, we will look into it and will provide you information accordingly.

private static void ImageMailMerge() throws Exception<o:p></o:p>

{<o:p></o:p>

<o:p></o:p>


//
Open template
<o:p></o:p>


com.aspose.words.Document doc = new com.aspose.words.Document(“E:/Data/ImageMailMerge.temp.docx”);<o:p></o:p>


//
Prepare dummy data for mail merge
<o:p></o:p>


String[] names = { “MyLogo” };<o:p></o:p>


Object[] values = { Files.readAllBytes(Paths.get(“E:/data/Aspose.jpg”)) };<o:p></o:p>


//
Add merge image field event handler
<o:p></o:p>


doc.getMailMerge().setFieldMergingCallback(new
MergeImageFieldResizeImage());<o:p></o:p>


//
Execute mail merge
<o:p></o:p>


doc.getMailMerge().execute(names, values);<o:p></o:p>


//
Save output document
<o:p></o:p>


doc.save(“E:/Data/Image_Mailmerge_out_resize.docx”);<o:p></o:p>

}<o:p></o:p>

private static class MergeImageFieldResizeImage
implements
IFieldMergingCallback {<o:p></o:p>

public void
fieldMerging(FieldMergingArgs args) throws Exception {<o:p></o:p>

// Do nothing.<o:p></o:p>

}<o:p></o:p>

/**<o:p></o:p>

* This is called when mail merge engine
encounters Image:
XXX merge<o:p></o:p>

* field in the document. You have a chance to
return an Image object,
<o:p></o:p>

* file name or a stream that contains the
image.
<o:p></o:p>

*/<o:p></o:p>

public void
imageFieldMerging(ImageFieldMergingArgs e) throws Exception {<o:p></o:p>

<o:p></o:p>

// Create DocuemntBuilder and move its
cursor to the mergefield
<o:p></o:p>


DocumentBuilder builder = new DocumentBuilder(e.getDocument());<o:p></o:p>


builder.moveToField(e.getField(), true);<o:p></o:p>


//
Insert image and specify its size
<o:p></o:p>


builder.insertImage((byte[])e.getFieldValue(), 50, 50);<o:p></o:p>


e.getField().remove();<o:p></o:p>

<o:p></o:p>

}<o:p></o:p>

}


Best Regards,