@judiciary What you are asking about is called Nested Mail Merge with Regions. You are right when you use XML data source, the relations between the table are build automatically while reading XML data into DataSet
. In your case you should create relations yourself. 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)
Alternatively, you can use implement IMailMergeDataSource and the appropriate getChildDataSource
method to return data source for the child region.