DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Edited on

Copy Contents from One Word Document to Another in Java

In some cases, you might need to copy partial or all contents from one Word document to another. In this article, I will demonstrate how to copy contents from one Word document to another, along with how to create a copy of a Word document programmatically in Java.

Contents Summary:

  • Copy Partial Contents from One Word document to Another
  • Copy All Contents from One Word Document to Another
  • Create a Copy of a Word document

Add Dependencies

In order to copy content from Word document, I will be using Free Spire.Doc for Java which is a free, multifunctional and easy-to-use API.
You can either download the jar of the library from the official website or install it from maven by adding the following code to your maven-based project:

<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>3.9.0</version>    
    </dependency>    
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Copy Partial Contents from One Word document to Another

You can copy partial contents, for example, a section or a paragraph from one Word document to another.

To copy a section from one document to another, you can use the Section.deepClone() method and the Document.getSections().add() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.SectionBreakType;

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

        //Load the source document
        Document sourceDoc = new Document();
        sourceDoc.loadFromFile("source.docx");

        //Get the first section
        Section sourceSection = sourceDoc.getSections().get(0);

        //If you want to add the section to the same page instead of a new page in the destination document, you need to set the section break type as no break.
        sourceSection.setBreakCode(SectionBreakType.No_Break);

        //Load the destination document
        Document destDoc = new Document();
        destDoc.loadFromFile("destination.docx");

        //Copy the first section from the source document to the destination document
        Section copySection = sourceSection.deepClone();
        destDoc.getSections().add(copySection);

        //Save the result document
        destDoc.saveToFile("CopyASection.docx", FileFormat.Docx_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Copy a section from one Word document to another

If you need to copy a paragraph, you can use the Paragraph.deepClone() and the Section.getParagraphs().add() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.interfaces.IParagraph;

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

        //Load the source document
        Document sourceDoc = new Document();
        sourceDoc.loadFromFile("source.docx");

        //Get the first section
        Section section1 = sourceDoc.getSections().get(0);
        //Get the first paragraph
        Paragraph para1 = section1.getParagraphs().get(0);

        //Load the destination document
        Document destDoc = new Document();
        destDoc.loadFromFile("destination.docx");

        //Get the last section of the destination document
        Section section2 = destDoc.getLastSection();

        //Copy the first paragraph from the source document to the destination document
        DocumentObject object = para1.deepClone();
        section2.getParagraphs().add((IParagraph) object);

        //Save the result document
        destDoc.saveToFile("CopyAParagraph.docx", FileFormat.Docx_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Copy a paragraph from one Word document to another

Copy All Contents from One Word Document to Another

You can use the Document.importContent() method to copy all contents from one Word document to another as shown in the following example.

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

public class CopyAllContents {
    public static void main(String []args){
        //Load the source document
        Document sourceDoc = new Document();
        sourceDoc.loadFromFile("source.docx");

        //Load the destination document
        Document destDoc = new Document();
        destDoc.loadFromFile("destination.docx");

        //Copy contents from the source document to the destination document
        destDoc.importContent(sourceDoc, true);

        //Save the result document
        destDoc.saveToFile("CopyAll.docx", FileFormat.Docx_2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Copy all contents from one Word document to another

Create a Copy of a Word document

To create a copy of a Word document, you can call the Document.deepClone() method.

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

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

        //Create a Document instance
        Document document = new Document();

        //Load a Word document
        document.loadFromFile("source.docx");

        //Clone the word document
        Document newDoc = document.deepClone();

        //Save the result file
        newDoc.saveToFile("Clone.docx", FileFormat.Docx_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Create a copy of a Word document

Top comments (0)