Types of Mail Merge Operation
The main idea of mail merge is to automatically create a document or multiple documents based on your template and data fetched from your data source. Aspose.Words allows you to perform two different types of mail merge operations: simple mail merge and mail merge with regions.
The most common example of using simple mail merge is when you want to send a document for different clients by including their names at the beginning of the document. To do this, you need to create merge fields such as First Name and Last Name in your template, and then fill them in with data from your data source. Whereas the most common example of using mail merge with regions is when you want to send a document that includes specific orders with the list of all items within each order. To do this, you will need to create merge regions inside your template – own region for each order, in order to fill it with all required data for the items.
The main difference between both merge operations is that simple mail merge (without regions) repeats the entire document per each data source record, whereas mail merge with regions repeats only designated regions per record. You can think of a simple mail merge operation as a particular case of merge with regions where the only region is the whole document.
Simple Mail Merge Operation
A simple mail merge is used to fill the mail merge fields inside your template with the required data from your data source (single table representation). So it is similar to the classic mail merge in Microsoft Word.
You can add one or more merge fields in your template and then execute the simple mail merge operation. It is recommended to use it if your template does not contain any merge regions.
The main limitation of using this type is the whole document content will be repeated for each record in the data source.
How to Execute a Simple Mail Merge Operation
Once your template is ready, you can start performing the simple mail merge operation. Aspose.Words allows you to execute a simple mail merge operation using different Execute methods that accept various data objects as the data source.
The following code example shows how to execute a simple mail merge operation using one of the Execute method:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Include the code for our template. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Create Merge Fields. | |
builder.insertField(" MERGEFIELD CustomerName "); | |
builder.insertParagraph(); | |
builder.insertField(" MERGEFIELD Item "); | |
builder.insertParagraph(); | |
builder.insertField(" MERGEFIELD Quantity "); | |
builder.getDocument().save(dataDir + "MailMerge.TestTemplate.docx"); | |
// Fill the fields in the document with user data. | |
doc.getMailMerge().execute(new String[] { "CustomerName", "Item", "Quantity" }, | |
new Object[] { "John Doe", "Hawaiian", "2" }); | |
builder.getDocument().save(dataDir + "MailMerge.Simple.docx"); |
You can notice the difference between the document before executing simple mail merge:

And after executing simple mail merge:

How to Create Multiple Merged Documents
In Aspose.Words, the standard mail merge operation fills only a single document with content from your data source. So, you will need to execute the mail merge operation multiple times to create multiple merged documents as an output.
The following code example shows how to generate multiple merged documents during a mail merge operation:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public class ProduceMultipleDocumentsDuringMailMerge { | |
private static final String dataDir = Utils.getSharedDataDir(ProduceMultipleDocumentsDuringMailMerge.class) + "MailMerge/"; | |
public static void main(String[] args) throws Exception { | |
produceMultipleDocuments(dataDir, "TestFile.doc"); | |
} | |
public static void produceMultipleDocuments(String dataDir, String srcDoc) throws Exception { | |
// Open the database connection. | |
ResultSet rs = getData(dataDir, "SELECT * FROM Customers"); | |
// Open the template document. | |
Document doc = new Document(dataDir + srcDoc); | |
// A record of how many documents that have been generated so far. | |
int counter = 1; | |
// Loop though all records in the data source. | |
while (rs.next()) { | |
// Clone the template instead of loading it from disk (for speed). | |
Document dstDoc = (Document) doc.deepClone(true); | |
// Extract the data from the current row of the ResultSet into a Hashtable. | |
Hashtable dataMap = getRowData(rs); | |
// Execute mail merge. | |
dstDoc.getMailMerge().execute(keySetToArray(dataMap), dataMap.values().toArray()); | |
// Save the document. | |
dstDoc.save(MessageFormat.format(dataDir + "TestFile Out {0}.doc", counter++)); | |
System.out.println("Counter : "); | |
} | |
} | |
/** | |
* Creates a Hashtable from the name and value of each column in the current | |
* row of the ResultSet. | |
*/ | |
public static Hashtable getRowData(ResultSet rs) throws Exception { | |
ResultSetMetaData metaData = rs.getMetaData(); | |
Hashtable values = new Hashtable(); | |
for (int i = 1; i <= metaData.getColumnCount(); i++) { | |
values.put(metaData.getColumnName(i), rs.getObject(i)); | |
} | |
return values; | |
} | |
/** | |
* Utility function that returns the keys of a Hashtable as an array of | |
* Strings. | |
*/ | |
public static String[] keySetToArray(Hashtable table) { | |
return (String[]) table.keySet().toArray(new String[table.size()]); | |
} | |
/** | |
* Utility function that creates a connection to the Database. | |
*/ | |
public static ResultSet getData(String dataDir, String query) throws Exception { | |
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); | |
String connectionString = "jdbc:ucanaccess://" + dataDir + "Customers.mdb"; | |
// DSN-less DB connection. | |
Connection connection = DriverManager.getConnection(connectionString); | |
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); | |
return statement.executeQuery(query); | |
} | |
} |
Mail Merge with Regions
You can create different regions in your template to have special areas that you can simply fill with your data. Use the mail merge with regions if you want to insert tables, rows with repeating data to make your documents dynamically grow by specifying those regions within your template.
You can create nested (child) regions as well as merge regions. The main advantage of using this type is to dynamically increase parts inside a document. See more details in the next article “Nested Mail Merge with Regions”.
How to Execute Mail Merge with Regions
A mail merge region is a specific part inside a document that has a start point and an end point. Both points are represented as mail merge fields that have specific names “TableStart:XXX” and “TableEnd:XXX”. All content that is included in a mail merge region will automatically be repeated for every record in the data source.
Aspose.Words allows you to execute mail merge with regions using different Execute methods that accept various data objects as the data source.
As a first step, we need to create the DataSet to pass it later as an input parameter to the ExecuteWithRegions method:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static DataSet CreateDataSet() { | |
// Create the customers table. | |
DataTable tableCustomers = new DataTable("Customers"); | |
tableCustomers.getColumns().add("CustomerID"); | |
tableCustomers.getColumns().add("CustomerName"); | |
tableCustomers.getRows().add(new Object[] { 1, "John Doe" }); | |
tableCustomers.getRows().add(new Object[] { 2, "Jane Doe" }); | |
// Create the orders table. | |
DataTable tableOrders = new DataTable("Orders"); | |
tableOrders.getColumns().add("CustomerID"); | |
tableOrders.getColumns().add("ItemName"); | |
tableOrders.getColumns().add("Quantity"); | |
tableOrders.getRows().add(new Object[] { 1, "Hawaiian", 2 }); | |
tableOrders.getRows().add(new Object[] { 2, "Pepperoni", 1 }); | |
tableOrders.getRows().add(new Object[] { 2, "Chicago", 1 }); | |
// Add both tables to a data set. | |
DataSet dataSet = new DataSet(); | |
dataSet.getTables().add(tableCustomers); | |
dataSet.getTables().add(tableOrders); | |
// The "CustomerID" column, also the primary key of the customers table is the | |
// foreign key for the Orders table. | |
dataSet.getRelations().add(tableCustomers.getColumns().get("CustomerID"), | |
tableOrders.getColumns().get("CustomerID")); | |
return dataSet; | |
} |
The following code example shows how to execute mail merge with regions using the ExecuteWithRegions(DataSet) method:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// The start point of mail merge with regions the dataset. | |
builder.insertField(" MERGEFIELD TableStart:Customers"); | |
// Data from rows of the "CustomerName" column of the "Customers" table will go | |
// in this MERGEFIELD. | |
builder.write("Orders for "); | |
builder.insertField(" MERGEFIELD CustomerName"); | |
builder.write(":"); | |
// Create column headers | |
builder.startTable(); | |
builder.insertCell(); | |
builder.write("Item"); | |
builder.insertCell(); | |
builder.write("Quantity"); | |
builder.endRow(); | |
// We have a second data table called "Orders", which has a many-to-one | |
// relationship with "Customers" | |
// picking up rows with the same CustomerID value. | |
builder.insertCell(); | |
builder.insertField(" MERGEFIELD TableStart:Orders"); | |
builder.insertField(" MERGEFIELD ItemName"); | |
builder.insertCell(); | |
builder.insertField(" MERGEFIELD Quantity"); | |
builder.insertField(" MERGEFIELD TableEnd:Orders"); | |
builder.endTable(); | |
// The end point of mail merge with regions. | |
builder.insertField(" MERGEFIELD TableEnd:Customers"); | |
// Pass our dataset to perform mail merge with regions. | |
DataSet customersAndOrders = CreateDataSet(); | |
doc.getMailMerge().executeWithRegions(customersAndOrders); | |
// Save the result | |
doc.save(dataDir + "MailMerge.ExecuteWithRegions.docx"); |
You can notice the difference between the document before executing mail merge with regions:

And after executing mail merge with regions:

Limitations of Mail Merge with Regions
There are some important points that you need to consider when performing a mail merge with regions:
- The start point TableStart:Orders and the end point TableEnd:Orders both need to be in the same row or cell. For example, if you start a merge region in a cell of a table, you must end the merge region in the same row as the first cell.
- The merge field name must match the column’s name in your DataTable. Unless you have specified mapped fields, the mail merge with regions will not be successful for any merge field that has a different name than the column’s name.
If one of these rules is broken, you will get unexpected results or an exception may be thrown.