Nested Mail Merge with Regions

In some scenarios, you may need to use nested Mail Merge with regions. Nested merge is a feature that allows you to merge hierarchical data from your data source into your merge template to easily populate your document. Basically, the hierarchical data is represented as a set of data items, and hierarchical relationships describe how the data items are related to each other (one item of data is the parent of another one).

Aspose.Words enables you to perform a Mail Merge operation with nested regions. You can use this feature if you have a data source that is organized into a tree-like structure and you want to execute a Mail Merge operation to populate a template with hierarchical data.

What is a Nested Mail Merge

The Mail Merge region is called nested if you have two or more Mail Merge regions where one of them is inside the other in a hierarchical form. Note that each region contains data from one table.

The most common example of a nested Mail Merge is an order that contains a few items where you need to link many data tables and present the information in a template.

The picture below shows two nested regions where the Order Mail Merge region is the parent of the Item Mail Merge region.

nested_mail_merge_with_regions

How to Process Mail Merge with Nested Regions

The data to be merged into a template can come from various sources, mainly relational databases or XML documents. In our example, we are going to use an SQLite database to store our data and load it with custom data source implementation.

The image below demonstrates how the data from the Order table passed to the nested merge regions will be linked to the Item table, as well as the output generated during the merge operation.

mail_merge_with_nested_regions

As you can see from the output document, each order from the Order table is inserted into the merge template with all order’s related items from the Item table. The next order will be inserted along with their items until all the orders and items are listed. The order of nesting Mail Merge with regions in the template must match the data relationships between the tables in the data source.

How to Create Data Relations from a Custom Data Source

Implement the IMailMergeDataSource interface to create relationships in the parent-child structure of your custom data source. Use the GetChildDataSource method to return the relevant child data of a current parent record.

The following code example demonstrates how to generate an invoice using nested Mail Merge with regions from SQLite database with SQLiteCpp: