The MailMerge class provides two events that could be very useful in expanding mail merge capabilities. The MailMerge.FieldMergingCallback property accepts a class which implements the methods IFieldMergingCallback.FieldMerging and IFieldMergingCallback.ImageFieldMerging. These can be used to implement custom control over the mail merge process.
The IFieldMergingCallback.FieldMerging event occurs during mail merge when a simple mail merge field is encountered in the document. This gives further control over the mail merge and you can perform any actions when the event occurs. This method is wrapped in a class that implements the IFieldMergingCallBack interface and accepts a FieldMergingArgs object that provides data for the corresponding event.
Example
Demonstrates how to implement custom logic in the MergeField event to apply cell formatting.
[Java]
public void mailMergeAlternatingRows() throws Exception {
Document doc = new Document(getMyDir() + "MailMerge.AlternatingRows.doc");
// Add a handler for the MergeField event.
doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldAlternatingRows());
// Execute mail merge with regions.
com.aspose.words.DataTable dataTable = getSuppliersDataTable();
doc.getMailMerge().executeWithRegions(dataTable);
doc.save(getMyDir() + "MailMerge.AlternatingRows Out.doc");
}
private class HandleMergeFieldAlternatingRows implements IFieldMergingCallback {
/**
* Called for every merge field encountered in the document.
* We can either return some data to the mail merge engine or do something
* else with the document. In this case we modify cell formatting.
*/
public void fieldMerging(FieldMergingArgs e) throws Exception {
if (mBuilder == null)
mBuilder = new DocumentBuilder(e.getDocument());
// This way we catch the beginning of a new row.
if (e.getFieldName().equals("CompanyName")) {
// Select the color depending on whether the row number is even or odd.
Color rowColor;
if (isOdd(mRowIdx))
rowColor = new Color(213, 227, 235);
else
rowColor = new Color(242, 242, 242);
// There is no way to set cell properties for the whole row at the moment,
// so we have to iterate over all cells in the row.
for (int colIdx = 0; colIdx < 4; colIdx++) {
mBuilder.moveToCell(0, mRowIdx, colIdx, 0);
mBuilder.getCellFormat().getShading().setBackgroundPatternColor(rowColor);
}
mRowIdx++;
}
}
public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
// Do nothing.
}
private DocumentBuilder mBuilder;
private int mRowIdx;
}
/*
* Returns true if the value is odd; false if the value is even.
*/
private static boolean isOdd(int value) throws Exception {
return (value % 2 != 0);
}
/**
* Create DataTable and fill it with data.
* In real life this DataTable should be filled from a database.
*/
private static com.aspose.words.DataTable getSuppliersDataTable() throws Exception {
java.sql.ResultSet resultSet = createCachedRowSet(new String[]{"CompanyName", "ContactName"});
for (int i = 0; i < 10; i++)
addRow(resultSet, new String[]{"Company " + Integer.toString(i), "Contact " + Integer.toString(i)});
return new com.aspose.words.DataTable(resultSet, "Suppliers");
}