DEV Community

SirOnly
SirOnly

Posted on

Merge Word Documents to Create a New Word Document

Merge Word Documents
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 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 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.
Document mergedDocument = new Document();
Enter fullscreen mode Exit fullscreen mode
  • 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");
Enter fullscreen mode Exit fullscreen mode
  • Insert the contents of the source documents into the merged document using the insertDocument() method.
mergedDocument.insertDocument(doc1, FileFormat.Auto);
mergedDocument.insertDocument(doc2, FileFormat.Auto);
Enter fullscreen mode Exit fullscreen mode
  • Save the merged document using the saveToFile() method.
mergedDocument.saveToFile("path/to/merged.docx", FileFormat.Auto);
Enter fullscreen mode Exit fullscreen mode

Full Code Example:

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

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:

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

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:

Top comments (0)