Copy Contents from One Word Document to Another in Java
Tags: java, word, copycontents
Published: 2021-12-10 09:54:13

---
title: Copy Contents from One Word Document to Another in Java
published: true
description: This article shows how to copy contents from one Word document to another in Java.
tags: Java, Word, copy#contents
//cover_image: https://direct_url_to_image.jpg
---
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](https://www.e-iceblue.com/Introduce/free-doc-component.html) which is a free, multifunctional and easy-to-use API. 
You can either download the jar of the library from the [official website](https://www.e-iceblue.com/Download/doc-for-java-free.html) or install it from maven by adding the following code to your maven-based project:
```java
<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>
```
##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.
```java
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);
    }
}
``` 
![Copy a section from one Word document to another](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pkoq7to3x3r0kb23ad5.png)

If you need to copy a paragraph, you can use the **Paragraph.deepClone()** and the **Section.getParagraphs().add()** method.
```java
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);
    }
}
```
![Copy a paragraph from one Word document to another](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2gd8ejnajd8jbv8f4dd.png) 
##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.
```java
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);
    }
}
```
![Copy all contents from one Word document to another](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qx9p5431lsm2duwe9jjy.png) 
##Create a Copy of a Word document##
To create a copy of a Word document, you can call the **Document.deepClone()** method.
```java
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);
    }
}
```
![Create a copy of a Word document](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/coxvh6kyb7tqrka2pi06.png)