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
{
    //Open template
    com.aspose.words.Document doc = new com.aspose.words.Document("E:/Data/ImageMailMerge.temp.docx");
    // Prepare dummy data for mail merge
    String[] names = { "MyLogo" };
    Object[] values = { Files.*readAllBytes*(Paths.*get*("E:/data/Aspose.jpg")) };
    //Add merge image field event handler
    doc.getMailMerge().setFieldMergingCallback(new MergeImageFieldResizeImage());
    // Execute mail merge
    doc.getMailMerge().execute(names, values);
    // Save output document
    doc.save("E:/Data/Image_Mailmerge_out_resize.docx");
}
private static class MergeImageFieldResizeImage implements IFieldMergingCallback {

    public void fieldMerging(FieldMergingArgs args) throws Exception {
        // Do nothing.
    }

    /**
    * This is called when mail merge engine encounters Image:**XXX** merge
    * field in the document. You have a chance to return an Image object,
    * file name or a stream that contains the image.
    */
    public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception {

        // Create DocuemntBuilder and move its cursor to the mergefield
        DocumentBuilder builder = new DocumentBuilder(e.getDocument());
        builder.moveToField(e.getField(), true);
        // Insert image and specify its size
        builder.insertImage((byte[])e.getFieldValue(), 50, 50);
        e.getField().remove();
    }
}

Best Regards,