For most people, when you want to combine multiple Word documents into one, what you’ll often do is copying content from each and pasting it into another document. To be honest, this method is time-consuming, especially when dealing with several documents or complex formatting. Here I’ll introduce a more efficient and convenient idea that merge Word documents in Java programmatically.
Before typing codes, please add Spire.Doc.jar into your project as a dependency. There are two method to do it. One is that download Free Spire.Doc for Java package from the link, unzip it and find Spire.Doc.jar in the “lib” folder. Finally add Spire.Doc.jar into IDEA. The other is that install Free Spire.Doc for Java from Maven repository. Create a Maven program, type the following codes in the pom.xml file, and click the button “Import Changes”.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
Using the code
Free Spire.Doc for Java provides an insertTextFromFile() method which supports merging Word
documents by inserting content from a document into another document. In the resulting document,
the inserted content will start from a new page.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class MergeFiles1 {
public static void main(String[] args) {
//Load the first Word Document
Document document = new Document("C:\\Users\\Test1\\Desktop\\Sample1.docx");
//Using insertTextFromFile method to insert the second document into the first document
document.insertTextFromFile("C:\\Users\\Test1\\Desktop\\Sample2.docx", FileFormat.
Docx_2013);
//Save the resulting document
document.saveToFile("Output/MergeFiles1.docx", FileFormat.Docx_2013);
}
}
Output
Besides, if you want the inserted content to follow the last paragraph of the destination document
instead of starting from a new page, use the following method to clone sections of the source document
and then append to the last section of the destination document.
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
public class MergeFiles2 {
public static void main(String[] args) {
//Load the first Word document
Document document1 = new Document("C:\\Users\\Test1\\Desktop\\Sample1.docx");
//Load the second Word document
Document document2 = new Document("C:\\Users\\Test1\\Desktop\\Sample2.docx");
//Get the last section of the first document
Section lastSection = document1.getLastSection();
//Add the sections of the second document to the last section of the first document
for (Section section:(Iterable <Section>)document2.getSections()) {
for (DocumentObject obj:(Iterable <DocumentObject>)section.getBody().getChildObjects()
) {
lastSection.getBody().getChildObjects().add(obj.deepClone());
}
}
//Save the resulting document
document1.saveToFile("Output/MergeFiles2.docx", FileFormat.Docx_2013);
}
}
Output