This simple topic describes how to merge Word documents using Java. It covers a step-by-step process to set up the environment along with links to required resources, and a working example code to develop an application that can concatenate Word files using Java. The developed application can be used inside any Java supported environment in operating systems like Linux, MS Windows, or macOs.
Steps to Merge Word Documents in Java
- Configure the IDE to use Aspose.Words for Java to append Word document in Java
- Access the Target Word document using the Document class instance
- Access the Target Word document using the Document class instance
- Append the Source DOCX into the Target DOCX file using the appendDocument method
- Save the combined Word document on the disk
The above-mentioned stepwise procedure explains the process to combine Word documents in Java. The process is initiated by loading the source and target DOCX document files from the disk using two separate Document class instances. The appendDocument method of the target’s document class object is then used to merge the source document inside it, which is then followed by saving the combined Word document file on the disk.
Code to Combine Word Documents using Java
import com.aspose.words.Document; | |
import com.aspose.words.ImportFormatMode; | |
import com.aspose.words.License; | |
public class AppendWordFiles { | |
public static void main(String[] args) throws Exception {// Merge Word files | |
String path ="/Users/KnowledgeBase/TestData/"; | |
// Apply Aspose.Words for Java license to append the Word documents | |
License licenseForMergeDocs = new License(); | |
licenseForMergeDocs.setLicense(path + "Conholdate.Total.Product.Family.lic"); | |
// Open the source Word file using the Document class | |
Document inputDocx = new Document(path + "Test1.docx"); | |
inputDocx.getFirstSection().getBody().appendParagraph("Source document text. "); | |
Document destDoc = new Document(path + "Test2.docx"); | |
destDoc.getFirstSection().getBody().appendParagraph("Destination document text. "); | |
// Now merge the document to the target document and | |
// preserving its formatting and saving on the disk | |
destDoc.appendDocument(inputDocx, ImportFormatMode.KEEP_SOURCE_FORMATTING); | |
destDoc.save(path + "Document.AppendDocument.docx"); | |
System.out.println("Done"); | |
} | |
} |
This example code demonstrates the process to merge Word documents in Java using a simple API interface and can easily be extended to merge multiple DOCX files together. The appendDocument method makes use of ImportFormatMode enum as a method argument, which offers provision to merge the incoming Word document using different formatting options including using the destination formatting, the source formatting, or keeping only different styles.
This topic has taught us how to append Word Documents using Java. If you are interested in digitally signing a Word document, refer to the article on how to digitally sign a Word Document using Java.