Merge Word Documents to Create a New Word Document
Tags: java, word, office, microsoft
Published: 2023-07-13 08:49:40

![Merge Word Documents](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58o77u2cj677sqjqv8om.png)
Merging Word documents is a common task that many people face when working with Microsoft Word. It involves combining the content of two or more Word documents into a single document. This can be useful when you need to create a report, a book, or any other document that requires content from multiple sources. In this article, we will explore how to use [Free Spire.Doc for Java](https://www.e-iceblue.com/Introduce/free-doc-for-java.html) to **merge Word documents**.

### Adding Free Spire.Doc for Java to the Program
Before using Free Spire.Doc for Java, you need to add it as a dependency to your Java project. The following are the steps to add Free Spire.Doc for Java as a dependency using Maven:

- Open your project in your preferred Integrated Development Environment (IDE).
- Right-click on the project and select "Add Dependency".
- In the "Search" field, type "Free Spire.Doc for Java".
- Select the latest version of Free Spire.Doc for Java and click "Add".

Alternatively, you can download the Free Spire.Doc for Java JAR file from the [official website](https://www.e-iceblue.com/Download/doc-for-java-free.html) and add it to your project's classpath.

## Using Free Spire.Doc for Java to Merge Word Documents
Free Spire.Doc for Java provides two methods for merging Word documents: merging by inserting documents and merging by cloning contents.

### Merging by Inserting Documents
This method involves merging Word documents by inserting them into a new document. The following are the steps to merge Word documents using this method:

- Create a new instance of the Document class for the new merged document.
```java
Document mergedDocument = new Document();
```

- Load the source Word documents using the **Document** class.
```java
Document doc1 = new Document("path/to/doc1.docx");
Document doc2 = new Document("path/to/doc2.docx");
```
- Insert the contents of the source documents into the merged document using the **insertDocument()** method.
```java
mergedDocument.insertDocument(doc1, FileFormat.Auto);
mergedDocument.insertDocument(doc2, FileFormat.Auto);
```

- Save the merged document using the **saveToFile()** method.
```java
mergedDocument.saveToFile("path/to/merged.docx", FileFormat.Auto);
```

**Full Code Example:**

```java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

public class MergeDocumentsByInserting {
    public static void main(String[] args) {
        // Create a new instance of the Document class for the new merged document
        Document mergedDocument = new Document();

        // Load the source Word documents using the Document class
        Document doc1 = new Document("path/to/doc1.docx");
        Document doc2 = new Document("path/to/doc2.docx");

        // Insert the contents of the source documents into the merged document
        mergedDocument.insertDocument(doc1, FileFormat.Auto);
        mergedDocument.insertDocument(doc2, FileFormat.Auto);

        // Save the merged document
        mergedDocument.saveToFile("path/to/merged.docx", FileFormat.Auto);
    }
}
```

### Merging by Cloning Contents
This method involves merging Word documents by cloning the contents of the source documents and adding them to a new document. The following are the steps to merge Word documents using this method:

- 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.

**Full Code Example:**

```java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

public class MergeDocumentsByCloning {
    public static void main(String[] args) {
        // Create a new instance of the Document class for the new merged document
        Document mergedDocument = new Document();

        // Load the source Word documents using the Document class
        Document doc1 = new Document("path/to/doc1.docx");
        Document doc2 = new Document("path/to/doc2.docx");

        // Clone the sections of the source documents and add them to the merged document
       Section section1 = doc1.getSections().get(0).clone();
        Section section2 = doc2.getSections().get(0).clone();
        mergedDocument.getSections().add(section1);
        mergedDocument.getSections().add(section2);

        // Save the merged document
        mergedDocument.saveToFile("path/to/merged.docx", FileFormat.Docx_2013);
    }
}
```

In addition to merging Word documents, Free Spire.Doc for Java provides a wide range of functions to help you create, read, edit, and manipulate Microsoft Word documents in Java applications. Some of the other functions of Free Spire.Doc for Java include:

- [Creating tables](https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Table/Create-Table-in-Word-in-Java.html)
- [Working with headers and footers](https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Header-and-Footer/Insert-Header-and-Footer-to-Word-in-Java.html)
- [Converting Word documents to HTML](https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Conversion/Java-Convert-Word-to-HTML.html)
- [Protecting Word documents with passwords](https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Security/Encrypt-and-Decrypt-Word-Document-in-Java.html)