Logo

dev-resources.site

for different kinds of informations.

Merge Word Documents to Create a New Word Document

Published at
7/13/2023
Categories
java
word
office
microsoft
Author
sironly
Categories
4 categories in total
java
open
word
open
office
open
microsoft
open
Author
7 person written this
sironly
open
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:

office Article's
30 articles in total
Favicon
Excel | Merged Cells Broken Up? Excel Keeps Crashing? Try to Update!
Favicon
Office Interior Excellence in Chennai: The Concept Ventures Advantage
Favicon
Elevate Your Business: Why Engaging an Office Interior Designer in Gurgaon Matters
Favicon
Dataprius - Intranet in cloud for companies. Store and work in cloud
Favicon
Choosing the standard office colour for maximum productivity and comfort
Favicon
Security and Privacy: Protecting Your Data During Titan Email to Office 365 Migration
Favicon
How To Use Copilot to Easily Create PowerPoint Presentations In Minutes
Favicon
Empower Your Remote Team: Enhancing Collaboration with Work From Home Monitoring Software
Favicon
Office Renovation
Favicon
Maximizing Workplace Efficiency with Application and URL Tracking Software
Favicon
How We are using office furniture?
Favicon
Best office furniture shop in Dubai
Favicon
Value for Money with an Office Mesh Back Chair
Favicon
Crafting Your Dream Workspace with Local Office Furniture Makers
Favicon
The Benefits of an Office Mesh Back Chair
Favicon
Buy Microsoft Software License Keys
Favicon
Testhouse Expands Global Footprint with New Office Launch in Saudi Arabia
Favicon
Practical Tips for Back Office Outsourcing
Favicon
The Essential Checklist for Successful Office Removals in London
Favicon
Fixing MSVCP140_ATOMIC_WAIT.dll Not Found
Favicon
[Java] Einfügen oder Lesen von Formeln und Funktionen in Excel
Favicon
Why Flexospaces is the Top Choice for Coworking in Pune
Favicon
Merge Word Documents to Create a New Word Document
Favicon
Office Space in Gothenburg
Favicon
How to Import Kerio to Office 365 in Easy Steps?
Favicon
How To Open Orphan OST File In Outlook Online?
Favicon
How To Download The Complete Office 365 Mailbox In Hard Drive?
Favicon
Enxurrada de Bits: Inscrições abertas para formação 100% gratuita em Programação, Robótica e mais!
Favicon
在 VBA 中讀取 UTF8 編碼的文字檔
Favicon
在 Word 和 PowerPoint 中幫程式加上行號

Featured ones: