Articles in this section
Category / Section

How to mail merge Word document in Java

3 mins read

Mail merge is a process of merging data from a data source to a Word template document. Syncfusion® Java Word library (Essential® DocIO) is used to generate reports like an invoice, payroll, letter, and more, by performing a mail merge faster in a batch process without Microsoft Word or interop dependencies. Using this library, you can mail merge the Word documents in a Java application.

Steps to Mail merge a Word document in Java:

  1. Create a new MailMergeWordDocument.java file and include the following namespaces.

JAVA

import com.syncfusion.docio.*;
  1. Use the following code sample to mail merge a Word document.

JAVA

// Create a WordDocument instance.
WordDocument document = new WordDocument();
String basePath = getDataDir("LetterTemplate.docx");
// Open the template Word document.
document.open(basePath, FormatType.Docx);
String[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone" };
String[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA",
"(206) 555-9857-x5467" };
// Perform mail merge.
document.getMailMerge().execute(fieldNames, fieldValues);
// Save the Word document.
document.save("Result.docx");
// Close the Word document.
document.close();

A complete working example of a mail merge Word document in Java can be downloaded from GitHub.

By executing the program, you will get the output Word document as follows.

Mail merge output Word document

You can refer to this link to know about how to run the Syncfusion® Java Word library (Essential® DocIO).

Take a moment to peruse the documentation. You can find basic Word document processing options along with the features like mail merge, merge, and split documents.

Explore more about a rich set of Syncfusion® Word Framework features.

A Github example to perform a mail merge in Word document in Java.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied