The merger of multiple MS Word documents could be needed in various scenarios such as reducing the number of documents, keeping similar kinds of content in a single file (i.e. invoices), and etc. Many online applications let you merge two or more MS Word documents, however, you may need to implement document merging features within your own web or desktop applications. For such a case, in this article, you will learn how to merge MS Word DOCX documents using Java.
- Java API for Merging MS Word Documents
- Merge MS Word DOCX Documents using Java
- Additional Options to Merge MS Word Documents
Merge Word Documents with Java API
Aspose.Words for Java is a popular word processing API that lets you create, manipulate, and play with MS Word document formats within Java applications. Along with a wide range of document generation features, the API allows you to merge MS Word documents quite easily. You can download the API or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.8</version>
<type>pom</type>
</dependency>
Merge MS Word DOCX Documents using Java
Merging MS Word documents using Aspose.Words for Java is as easy as pie. Just load the documents and merge them by calling a single method. Below are the two MS Word documents that you will merge in the following code samples.
The following are the steps to merge two MS Word documents.
- Load the destination Word document using the Document class.
- Load the source Word document using the Document class.
- Merge source Word document into destination document using Document.appendDocument(Document, ImportFormatMode) method.
- Set ImportFormatMode in the appendDocument method to specify the formatting of the source document.
- Save the destination Word document using Document.save(String, SaveFormat) method.
The following code sample shows how to merge MS Word documents (DOCX) using Java.
The following is the screenshot of the merged MS Word document.
Merge MS Word DOCX with Additional Options
Aspose.Words for Java also provides some additional options to customize the appearance of the source document after performing the merger. The ImportFormatOptions class provides the following customization options.
- setIgnoreHeaderFooter (boolean value) – Specifies that source formatting of headers/footers content is ignored if ImportFormatMode.KEEP_SOURCE_FORMATTING mode is used.
- setIgnoreTextBoxes (boolean value) – Specifies that source formatting of textboxes content is ignored if ImportFormatMode.KEEP_SOURCE_FORMATTING mode is used.
- setKeepSourceNumbering (boolean value) – Specifies how to import numbering when it clashes in source and destination documents.
- setSmartStyleBehavior (boolean value) – Specifies how to import styles when having similar names in source and destination documents.
The following are the steps to merge two MS Word documents with additional options.
- Load the destination Word document using the Document class.
- Load the source Word document using the Document class.
- Create an instance of ImportFormatOptions class and set the desired options.
- Merge source Word document into destination document using Document.appendDocument(Document, ImportFormatMode, ImportFormatOptions) method.
- Save the destination Word document using Document.save(String, SaveFormat) method.
The following code sample shows how to merge two MS Word documents with additional options using Java.
Conclusion
In this article, you have seen how to merge MS Word DOCX documents using Java. Furthermore, you have learned how to use the additional options to customize the document merging feature. You can explore more about Aspose.Words for Java using the documentation.