Last active
December 11, 2020 16:30
-
-
Save aspose-com-gists/ef6c4280ab216d12222a5c252b936892 to your computer and use it in GitHub Desktop.
Mail Merge in Word using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create document builder | |
DocumentBuilder builder = new DocumentBuilder(); | |
// Insert a text input field the unique name of this field is "Hello", the other parameters define | |
// what type of FormField it is, the format of the text, the field result and the maximum text length (0 = no limit) | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", "Hello", 0); | |
builder.insertField("MERGEFIELD CustomerFirstName \\* MERGEFORMAT"); | |
builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "", " ", 0); | |
builder.insertField("MERGEFIELD CustomerLastName \\* MERGEFORMAT"); | |
builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "", " , ", 0); | |
// Insert a paragraph break into the document | |
builder.insertParagraph(); | |
// Insert mail body | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", "Thanks for purchasing our ", 0); | |
builder.insertField("MERGEFIELD ProductName \\* MERGEFORMAT"); | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", ", please download your Invoice at ", | |
0); | |
builder.insertField("MERGEFIELD InvoiceURL \\* MERGEFORMAT"); | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", | |
". If you have any questions please call ", 0); | |
builder.insertField("MERGEFIELD Supportphone \\* MERGEFORMAT"); | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", ", or email us at ", 0); | |
builder.insertField("MERGEFIELD SupportEmail \\* MERGEFORMAT"); | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", ".", 0); | |
builder.insertParagraph(); | |
// Insert mail ending | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", "Best regards,", 0); | |
builder.insertBreak(BreakType.LINE_BREAK); | |
builder.insertField("MERGEFIELD EmployeeFullname \\* MERGEFORMAT"); | |
builder.insertTextInput("TextInput1", TextFormFieldType.REGULAR, "", " ", 0); | |
builder.insertField("MERGEFIELD EmployeeDepartment \\* MERGEFORMAT"); | |
// Save document | |
builder.getDocument().save("document.docx"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApplyCustomFormattingDuringMailMerge { | |
private static final String dataDir = Utils.getSharedDataDir(ApplyCustomFormattingDuringMailMerge.class) + "MailMerge/"; | |
public static void main(String[] args) throws Exception { | |
Document doc = new Document(dataDir + "MailMerge.AlternatingRows.doc"); | |
// Add a handler for the MergeField event. | |
doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldAlternatingRows()); | |
// Execute mail merge with regions. | |
DataTable dataTable = getSuppliersDataTable(); | |
doc.getMailMerge().executeWithRegions(dataTable); | |
doc.save(dataDir + "MailMerge.AlternatingRows Out.doc"); | |
} | |
/** | |
* Returns true if the value is odd; false if the value is even. | |
*/ | |
public 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 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 DataTable(resultSet, "Suppliers"); | |
} | |
/** | |
* A helper method that creates an empty Java disconnected ResultSet with | |
* the specified columns. | |
*/ | |
private static ResultSet createCachedRowSet(String[] columnNames) throws Exception { | |
RowSetMetaDataImpl metaData = new RowSetMetaDataImpl(); | |
metaData.setColumnCount(columnNames.length); | |
for (int i = 0; i < columnNames.length; i++) { | |
metaData.setColumnName(i + 1, columnNames[i]); | |
metaData.setColumnType(i + 1, java.sql.Types.VARCHAR); | |
} | |
CachedRowSet rowSet = RowSetProvider.newFactory().createCachedRowSet(); | |
; | |
rowSet.setMetaData(metaData); | |
return rowSet; | |
} | |
/** | |
* A helper method that adds a new row with the specified values to a | |
* disconnected ResultSet. | |
*/ | |
private static void addRow(ResultSet resultSet, String[] values) throws Exception { | |
resultSet.moveToInsertRow(); | |
for (int i = 0; i < values.length; i++) | |
resultSet.updateString(i + 1, values[i]); | |
resultSet.insertRow(); | |
// This "dance" is needed to add rows to the end of the result set properly. | |
// If I do something else then rows are either added at the front or the result | |
// set throws an exception about a deleted row during mail merge. | |
resultSet.moveToCurrentRow(); | |
resultSet.last(); | |
} | |
} | |
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 (ApplyCustomFormattingDuringMailMerge.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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<customers> | |
<customer Name="John Ben Jan" ID="1" Domain="History" City="Boston"/> | |
<customer Name="Lisa Lane" ID="2" Domain="Chemistry" City="LA"/> | |
<customer Name="Dagomir Zits" ID="3" Domain="Heraldry" City="Milwaukee"/> | |
<customer Name="Sara Careira Santy" ID="4" Domain="IT" City="Miami"/> | |
</customers> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create document | |
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("MailMerge.ExecuteWithRegions.docx"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create the Dataset and read the XML | |
DataSet customersDs = new DataSet(); | |
customersDs.readXml("Customers.xml"); | |
// Open a template document | |
Document doc = new Document("TestFile XML.docx"); | |
// Execute mail merge to fill the template with data from XML using DataTable. | |
// Note that this class also works with a single repeatable region (and any nested regions). | |
// To merge multiple regions at the same time from a single XML data source, use the XmlMailMergeDataSet class. | |
// e.g doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData)); | |
doc.getMailMerge().execute(customersDs.getTables().get("Customer")); | |
// Save the output document | |
doc.save("generated-document.docx"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include the code for our template | |
Document doc = new Document(); | |
// Pass the document to document builder | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Create Merge Fields | |
builder.insertField(" MERGEFIELD CustomerName "); | |
builder.insertParagraph(); | |
builder.insertField(" MERGEFIELD Item "); | |
builder.insertParagraph(); | |
builder.insertField(" MERGEFIELD Quantity "); | |
// Save the template | |
builder.getDocument().save("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" }); | |
// Save the document | |
builder.getDocument().save("MailMerge.Simple.docx"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment