We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Change image layout in IFieldMergingCallback.imageFieldMerging

Hello,


Is it possible to change the image layout/position when we insert it using mail merge field and method IFieldMergingCallback.imageFieldMerging.

A request is set image property “text wrapping” to “Behind text”.

Best regards, Evgeniy

Hi Evgeniy,


Thanks for your inquiry. You can insert image at merge field’s location and resize it for example by using the following code:
Document doc = new Document(getMyDir() + “input.docx”);

doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(new String[]{“img”}, new Object[]{“D:\Temp\Aspose.Words.jpg”});

doc.save(getMyDir() + “15.10.0.docx”);


static class HandleMergeField implements IFieldMergingCallback
{
public void fieldMerging(FieldMergingArgs args) throws Exception
{
<font color="BLUE"><b>}</b></font>

<font color="RED"><b>public</b></font> <font color="RED"><b>void</b></font> imageFieldMerging<font color="BLUE"><b>(</b></font>ImageFieldMergingArgs args<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception
<font color="BLUE"><b>{</b></font>
    <font color="RED"><b>if</b></font> <font color="BLUE"><b>(</b></font>args<font color="BLUE"><b>.</b></font>getFieldName<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>equals<font color="BLUE"><b>(</b></font><font color="PURPLE">"img"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE"><b>{</b></font>
        Document doc <font color="BLUE">=</font> <font color="BLUE"><b>(</b></font>Document<font color="BLUE"><b>)</b></font> args<font color="BLUE"><b>.</b></font>getDocument<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        DocumentBuilder builder <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DocumentBuilder<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        builder<font color="BLUE"><b>.</b></font>moveToMergeField<font color="BLUE"><b>(</b></font>args<font color="BLUE"><b>.</b></font>getFieldName<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        Shape img <font color="BLUE">=</font> builder<font color="BLUE"><b>.</b></font>insertImage<font color="BLUE"><b>(</b></font>args<font color="BLUE"><b>.</b></font>getFieldValue<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>toString<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        img<font color="BLUE"><b>.</b></font>setWidth<font color="BLUE"><b>(</b></font><font color="BROWN">36</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        img<font color="BLUE"><b>.</b></font>setHeight<font color="BLUE"><b>(</b></font><font color="BROWN">36</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    <font color="BLUE"><b>}</b></font>
<font color="BLUE"><b>}</b></font>

}

Hope, this helps.

Best regards,
Hi Awais,

builder.moveToMergeField(args.getFieldName());
Shape img = builder.insertImage(args.getFieldValue().toString());

Thanks a lot! This is what I really needed.

Best regards, Evgeniy