Sometimes we need to merge several related Word documents to make a more complete one. To merge documents with MS Word, you need to manually copy and paste contents or import contents from other documents, which can be tedious. Luckily, Spire.Doc for Java provides 2 easy ways to merge Word documents by programming. This article will show the detailed steps to merge Word documents.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Merge Documents by Inserting the Entire File
The method Document.insertTextFromFile() provided by Spire.Doc allows merging Word documents by inserting other documents entirely into a document with the inserted contents starting from a new page.
The detailed steps of merging documents by inserting the entire file are as follows:
- Create an object of Document class and load a Word document from disk.
- Insert another Word document entirely to the loaded document using Document.insertTextFromFile() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*; public class merge { public static void main(String[] args) { //Create an object of Document and load a Word document from disk Document document = new Document("C:/Samples/Sample1.docx"); //Insert another Word document entirely to the document document.insertTextFromFile("C:/Samples/Sample2.docx", FileFormat.Docx_2013); //Save the result document document.saveToFile("MergingResult.docx", FileFormat.Docx_2013); } }
Merge Documents by Cloning Contents
If you want to merge documents without starting a new page, you can clone the contents of other documents to add to the end of a document.
The detailed steps of merging documents by cloning contents are as follows:
- Create two objects of Document and load the two Word documents from disk.
- Loop through the second document to get all the sections using Document.getSections() method, then loop through all the sections to get their child objects using Section.getBod().getChildObjects() method, then get the last section of the first document using Document.getLastSection() method, and then add the child objects to the last section of the first document using Body.getChildObjects().add() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*; public class mergeDocuments { public static void main(String[] args){ //Create two Document objects and load two Word documents from disk Document document1 = new Document("C:/Samples/Sample1.docx"); Document document2 = new Document("C:/Samples/Sample2.docx"); //Loop through the second document to get all the sections for (Object sectionObj : (Iterable) document2.getSections()) { Section sec=(Section)sectionObj; //Loop through the sections of the second document to get their child objects for (Object docObj :(Iterable ) sec.getBody().getChildObjects()) { DocumentObject obj=(DocumentObject)docObj; //Get the last section of the first document Section lastSection = document1.getLastSection(); //Add the child objects to the last section of the first document Body body = lastSection.getBody(); body.getChildObjects().add(obj.deepClone()); } } //Save the result document document1.saveToFile("MergingResult.docx", FileFormat.Docx_2013); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.