Merge Word Documents using Java

You can programmatically merge two or more Word documents into a single document. As a Java developer, you can easily merge Word documents in your Java applications. In this article, you will learn how to merge Word documents using Java.

The following topics are discussed/covered in this article:

Java API for Merging Word Documents

For merging DOCX files, I will be using GroupDocs.Merger for Java API. It allows you to develop high-performance applications that can combine, rip, shuffle, cut or delete pages, slides, and diagrams on the go.‎ It enables you to reorder or replace document pages, change page orientation, manage document passwords and perform other manipulations easily for the supported file formats such as Word, Excel, PDF, and PowerPoint.

You can download the JAR of the API or just add the following pom.xml configuration in your Maven-based Java applications to try the below-mentioned code examples.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-merger</artifactId>
        <version>20.11</version> 
</dependency>

Merge Word Documents using Java

You can easily merge two or more Word documents programmatically by following the simple steps mentioned below:

  • Create an instance of the Merger class
  • Specify the path to the input DOCX file
  • Call the join() method and specify the path to the target DOCX file
  • Repeat the above step and add more files to merge
  • Call the save() method and save the merged file

The following code sample shows how to merge multiple DOCX files using Java.

Merge Multiple Word Documents using Java

Merge Multiple Word Documents using Java

The Merger class is the main class that controls the document merging process. It provides various methods to join, extract, remove and split document pages.

The Join() method of the Merger class joins the two or more documents into one single document. It takes a document from the file path or from the input stream as an input parameter. You may also provide JoinOptions as well.

The save() method of the Merger class saves the resultant document to the provided file path. You may also save the document to the OutputStream.

Combine Specific Pages of Word Documents using Java

You can combine specific pages of Word documents programmatically by following the simple steps mentioned below:

  • Create an instance of the Merger class
  • Specify the path to the input DOCX file
  • Define JoinOptions and set the start and end page number
  • Call the join() method and specify the path to the target DOCX file
  • Call the save() method and save the merged file

The following code sample shows how to combine selected pages of Word documents using Java.

The JoinOptions class provides options such as start page number, end page number and mode for joining the documents.

Merge and Secure with Password using Java

You can merge two or more Word documents and then secure with a password programmatically by following the simple steps mentioned below:

  • Create an instance of the Merger class
  • Specify the path to the input DOCX file
  • Call the join() method and specify the path to the target DOCX file
  • Repeat the above step and add more files to merge
  • Set password using AddPasswordOptions
  • Call the addPassword() method with AddPasswordOptions
  • Call the save() method to save the password-protected merged file

The following code sample shows how to merge multiple DOCX files and then secure the merged file with a password using Java.

The AddPasswordOptions class provides options to set password for document protection.

The addPassword() method of the Merger class gets AddPasswordOptions as an input parameter and protects the document with a password.

Merge Word Document into PDF using Java

You can merge two or more Word documents into a PDF document programmatically by following the simple steps mentioned below:

  • Create an instance of the Merger class
  • Specify the path to the input PDF file
  • Call the join() method and specify the path to the target DOCX file
  • Call the save() method and save the merged PDF file

The following code sample shows how to merge DOCX files into a PDF file using Java.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to merge Word documents using Java. You can learn more about GroupDocs.Merger for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also