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

Free Support Forum - aspose.com

MailMerge with array of data

I have a scenario here.


I have a table in the attached doc in that table there are two rows one is header and one is value row.
And I have two mergefields defined as table.header and table.values. Now my values can be multiple based on that I want to create multiple rows in the table. Is it possible? I am giving you the sample code as well.

private static String MyDir = "src/quickstart/doc2pdf/data/";

public static void main(String[] args) throws Exception {

Document doc = new Document(MyDir + "Field.docx");

List values = new ArrayList();

// I want three value rows which should generate dynamically in the docx.

values.add("Value 1");

values.add("Value 2");

values.add("Value 3");

doc.getMailMerge().execute(

new String[] {"Demo", "Table.header", "Table.value"},

new Object[] {"James Bond", "Sample Header", values });

doc.save(MyDir + "FieldOut.docx");

}


While I run this code I got the output as attached but what I expected I am attaching as Expected.docx

Hi Abhradeep,


Thanks for your inquiry. You can achieve this using mail merge with regions functionality of Aspose.Words. I have modified your template document. Please see attachment with this post and try executing the following code snippet:
Document doc = new Document(getMyDir() + “Field-modified.docx”);

DataTable dataTable = new DataTable(“tbl”);

dataTable.getColumns().add(“table.value”);

DataRow row = dataTable.newRow();
row.set(0, “value 1”);
dataTable.getRows().add(row);

row = dataTable.newRow();
row.set(0, “value 2”);
dataTable.getRows().add(row);

row = dataTable.newRow();
row.set(0, “value 3”);
dataTable.getRows().add(row);

doc.getMailMerge().executeWithRegions(dataTable);

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

I hope, this helps.

Best regards,

Thanks this is working