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

Free Support Forum - aspose.com

Custom image insert during mail merge

Hi,


I was wondering how would I insert a custom image (jfreechart) generated from database data using the mail merge? I already have multiple table and DataSet associated with executeWithRegions().

My option would be to do this using DocumentBuilder and executing custom query at the end of the document generation function, but I was wondering if this can be done during mail merge and what would be your suggestion?

Thanks
Hi Branislav,

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

doc.getMailMerge().setFieldMergingCallback(new HandleMergeFields());
doc.getMailMerge().executeWithRegions(getDataTable());

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

static class HandleMergeFields implements IFieldMergingCallback
{
public void fieldMerging(FieldMergingArgs args) throws Exception {
DocumentBuilder builder = new DocumentBuilder(args.getDocument());
    <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">"pic"</font><font color="BLUE"><b>)</b></font> <font color="BLUE"><font color="BLUE">&</font><font color="BLUE">&</font></font> args<font color="BLUE"><b>.</b></font>getFieldValue<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">!</font><font color="BLUE">=</font> <font color="RED"><b>null</b></font><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>
        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>
    <font color="BLUE"><b>}</b></font>
<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 e<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font>
    <font color="GREEN"><i>// Do nothing.

}
}

private static DataTable getDataTable()
{
DataTable dataTable = new DataTable(“Sale”);

dataTable<font color="BLUE"><b>.</b></font>getColumns<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>add<font color="BLUE"><b>(</b></font><font color="RED"><b>new</b></font> DataColumn<font color="BLUE"><b>(</b></font><font color="PURPLE">"pic"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

DataRow dataRow<font color="BLUE"><b>;</b></font>
<font color="RED"><b>for</b></font> <font color="BLUE"><b>(</b></font><font color="RED"><b>int</b></font> i <font color="BLUE">=</font> <font color="BROWN">0</font><font color="BLUE"><b>;</b></font> i <font color="BLUE"><</font> <font color="BROWN">5</font><font color="BLUE"><b>;</b></font> i<font color="BLUE"><font color="BLUE">+</font><font color="BLUE">+</font></font><font color="BLUE"><b>)</b></font>
<font color="BLUE"><b>{</b></font>
    dataRow <font color="BLUE">=</font> dataTable<font color="BLUE"><b>.</b></font>newRow<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    dataRow<font color="BLUE"><b>.</b></font>set<font color="BLUE"><b>(</b></font><font color="PURPLE">"pic"</font><font color="BLUE"><b>,</b></font> getMyDir<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">+</font> <font color="PURPLE">"aspose.words.jpg"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    dataTable<font color="BLUE"><b>.</b></font>getRows<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>add<font color="BLUE"><b>(</b></font>dataRow<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
<font color="BLUE"><b>}</b></font>

<font color="RED"><b>return</b></font> dataTable<font color="BLUE"><b>;</b></font>

}

Hope, this helps.

Best regards,