Repeating merge fields inside another repeating merge field

Hi there,

I have a use case where in an html document I have two TableStart and TableEnd, one inside another. And have two merge fields. One inside parent TableStart:Header (“header mergefield”) and one inside inner TableStart:Content (“content mergefield”).

<<TableStart:Header>>
----header mergefield
----<<TableStart:Content>>
--------content mergefield
----<<TableEnd:Content>>
<<TableEnd:Header>>

The output I am trying to get out of this is:

Header 1
----Content a
----Content b

Header 2
----Content c
----Content d

Header 3
----Content e
----Content f

Any help how this can be implemented is very much appreciated.

Regards,
Vidyabhushan

@vapanchamukhi That you are asking about is called Nested Mail Merge with regions. Please see our documentation to learn how to achieve this:
https://docs.aspose.com/words/java/nested-mail-merge-with-regions/

For example see the following simple example:

// Define data set and data tables
DataSet ds = new DataSet();
DataTable root = new DataTable("root");
root.getColumns().add("id");
root.getColumns().add("title");
DataTable child = new DataTable("child");
child.getColumns().add("root_id");
child.getColumns().add("value");

// Add tables into the data set
ds.getTables().add(root);
ds.getTables().add(child);
// Define relation between the tables.
ds.getRelations().add(root.getColumns().get("id"), child.getColumns().get("root_id"));

// Put some dummy data into the tables.
// In your case data will be read from JSON.
root.getRows().add(1, "First Group");
root.getRows().add(2, "Second Group");
root.getRows().add(3, "Third Group");
child.getRows().add(1, "First item in the First Group");
child.getRows().add(1, "Second item in the First Group");
child.getRows().add(1, "Third item in the First Group");
child.getRows().add(2, "First item in the Second Group");
child.getRows().add(2, "Second item in the Second Group");
child.getRows().add(3, "Lonely item in the Third Group");

// Open template and execute mail merge with regions.
Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().executeWithRegions(ds);
doc.save("C:\\Temp\\out.docx");

in.docx (13.9 KB)
out.docx (11.4 KB)

In the code example DataSet is used a a data source. Alternatively, you can use implement IMailMergeDataSource and the appropriate getChildDataSource method to return data source for the child region. Or Load data from XML, when you use XML data source, the relations between the table are build automatically while reading XML data into DataSet.

Thanks for solving this problem. This resolved our issue.

1 Like