Mail merge for Word or PDF documents in Java

High-speed Java library for merging document templates and user data into files of various formats

Our solution provides a powerful platform independent API. Use our Mail merge document API to develop high-level software for Java platform. This is a powerful software solution for merging templates in Word and PDF formats and data from custom data sources and further exporting the result to DOCX, PDF, HTML and other popular formats using Java.

View code snippet

Mail merge using Java

With our Java solution you can create documents, personalized for each recipient, using the Mail merge feature in Java. To do this, just use the Mail merge template and data from the customer data source.

A merge template is a document with merge fields – special fields where you want personalized data from your data source to be inserted. As a merge template, use a PDF or Word document in DOC or DOCX format with special merge fields.

The data source is the data store from which data is pulled during the Mail merge operation to personalize your documents.

Generate reports

You can also build reports using the Mail merge operation. Just create a Mail merge template with merge fields that will be used for report generation and select the data source. You can even generate reports from excel data if you collect it there. So you see how easy it is to generate reports in Java.

Try to use Mail merge in Java

The following example shows how to perform a Mail merge operation in Java. To verify this, load a document template with merge fields and fill in these fields through our interface. After running the code, download the result of the Mail merge in a convenient format such as DOCX or PDF, generated in Java.

For example, you can perform Mail merge operation with Word document files such as DOC or DOCX and save the output to PDF:

Execute a Mail merge operation in Java
Upload a Mail merge template
Run code
Select the target format from the list
// Repository path: https://releases.aspose.com/java/repo/
// Maven, where 'ver' - Aspose.Words version number, for example, 24.4.
<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-words</artifactId>
  <version>ver</version>
  <classifier>jdk17</classifier>
</dependency>
Copy
// Repository path: https://releases.aspose.com/java/repo/
// Gradle, where 'ver' - Aspose.Words version number, for example, 24.4.
compile(group: 'com.aspose', name: 'aspose-words', version: 'ver', classifier: 'jdk17')
Copy
// Repository path: https://releases.aspose.com/java/repo/
// Ivy, where 'ver' - Aspose.Words version number, for example, 24.4.
<dependency org="com.aspose" name="aspose-words" rev="ver">
 <artifact name="aspose-words" m:classifier="jdk17" ext="jar"/>
</dependency>
Copy
// Repository path: https://releases.aspose.com/java/repo/
// Sbt, where 'ver' - Aspose.Words version number, for example, 24.4.
libraryDependencies += "com.aspose" % "aspose-words" % "ver"
Copy
import com.aspose.words.*;

String[] fields = new String[]
{
};
Object[] values = new Object[]
{
};

Document doc = new Document("Input.docx");
doc.getMailMerge().execute(fields, values);
doc.save("Output.docx");
Run code

How to use Mail merge in Word or PDF documents

  1. Install Aspose.Words for Java.
  2. Add a library reference (import the library) to your Java project.
  3. Create a merge template with merge fields.
  4. Open the template file in Java.
  5. Call the execute() method, passing the merge fields and their values there.
  6. Call the Save() method, passing an output filename with required extension.
  7. Get the result of Mail merge operation as a separate file.

Java library to use Mail merge

We host our Java packages in Maven repositories. 'Aspose.Words for Java' is a common JAR file containing byte-code. Please follow the step-by-step instructions on how to install it to your Java developer environment.

System Requirements

Java SE 7 and more recent Java versions are supported. We also provide a separate package for Java SE 6 in case you are obliged to use this outdated JRE.

Our Java package is cross-platform and runs on all operating systems with JVM implementation, including Microsoft Windows, Linux, macOS, Android and iOS.

For information on optional package dependencies, such as JogAmp JOGL, Harfbuzz font engine, Java Advanced Imaging JAI, please refer to Product Documentation.

Most popular formats for Mail merge operations

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.