DEV Community

CodeSharing
CodeSharing

Posted on

How to Merge Word Documents in Java Application

In daily work, it is inevitable that you will need to merge multiple Word documents into a single document to reorganize them. In order to help you complete this operation quickly and efficiently in Java application, this article will introduce two methods to merge Word documents by using Free Spire.Doc for Java.

#1: The inserted document starts from a new page.

#2: The inserted document follows the last paragraph of the destination document.

Installation

Method 1: Download the Free Spire.Doc for Java and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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>
Enter fullscreen mode Exit fullscreen mode

Example 1:
If you want the inserted content to start from a new page, you can use insertTextFromFile() method to insert content from a source document into a destination document.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class MergeWordDocument {
    public static void main(String[] args){

        //File path of the first document
        String filePath1 = "merge1.docx";
        //File path of the second document
        String filePath2 = "merge2.docx";

        //Load the first document
        Document document = new Document(filePath1);

        //Insert content of the second document into the first document
        document.insertTextFromFile(filePath2, FileFormat.Docx_2013);

        //Save the resultant document
        document.saveToFile("Merge.docx", FileFormat.Docx_2013);

    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Example 2:
If you want the inserted content to follow the last paragraph of the destination document, you can use the below 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 MergeWordDocument {
    public static void main(String[] args){

        //File path of the first document
        String filePath1 = "merge1.docx";
        //File path of the second document
        String filePath2 = "merge2.docx";

        //Load the first document
        Document document1 = new Document(filePath1);
        //Load the second document
        Document document2 = new Document(filePath2);

        //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 resultant document
        document1.saveToFile("MergeWordDocument.docx", FileFormat.Docx_2013);

    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)