Java copy Word document

This article introduces how to copy Word document in Java program. The copying methods are all copied in a format, and the code examples are shown from the following points:

  • Copy the body content of Word, which can support multiple elements including text, pictures, tables, hyperlinks, bookmarks, annotations, shapes, numbered lists, footnotes, endnotes, etc. When copying, you can copy the whole document content and the specified paragraph content
  • Copy the Word header and footer, including the text, pictures, page number fields, etc. in the header and footer
  • Copy Word watermark effect, including text watermark and image watermark

Tools: free fire.doc for Java Download jar package , and extract the jar file under the lib folder to import into the Java program, or download it through the maven warehouse Import . Refer to the following import effect:

The two documents used for testing are as follows. Copy the contents of the document on the left to the document on the right:

[example 1] copy the text of Word

1.1 copy the whole document

import com.spire.doc.*;

public class CopyDoc {
    public static void main(String[] args) {
        //Load document 1
        Document doc1 = new Document();
        doc1.loadFromFile("test.docx");

        //Load document 2
        Document doc2 = new Document();
        doc2.loadFromFile("target.docx");

        //Traverse all child objects in document 1
        for (int i = 0; i < doc1.getSections().getCount(); i++) {
            Section section = doc1.getSections().get(i);

            for( int j = 0;j< section.getBody().getChildObjects().getCount();j++)
            {
                Object object = section.getBody().getChildObjects().get(j);

                //Copy body content from document 1 and add to document 2
                doc2.getSections().get(0).getBody().getChildObjects().add(((DocumentObject) object).deepClone());
            }
        }

        //Save document 2
        doc2.saveToFile("CopyDoc.docx", FileFormat.Docx_2013);
        doc2.dispose();
    }
}

Copy effect (the copy effect here does not include watermark, header, footer, etc.):

1.2 copy specified paragraph

import com.spire.doc.*;

import com.spire.doc.documents.Paragraph;


public class CopyPara {
    public static void main(String[] args) {
        //Load document 1
        Document doc1 = new Document();
        doc1.loadFromFile("test.docx");

        //Get the third paragraph in document 1
        Section section1 = doc1.getSections().get(0);
        Paragraph paragraph = section1.getParagraphs().get(2);

        //Load document 2, get section
        Document doc2 = new Document();
        doc2.loadFromFile("target.docx");
        Section section2 = doc2.getSections().get(0);

        //Copy paragraphs from document 1 and add to document 2
        Paragraph newparagraph = (Paragraph) paragraph.deepClone();
        section2.getParagraphs().add(newparagraph);

        //Save document 2
        doc2.saveToFile("CopyPara.docx",FileFormat.Docx_2013);
        doc2.dispose();
    }
}

Paragraph copy result:

 

[example 2] copy Word header and footer

 

import com.spire.doc.*;

public class CopyHeaderFooter {
    public static void main(String[] args) {
        //Load document 1
        Document doc1 = new Document();
        doc1.loadFromFile("test.docx");

        //Obtain section
        Section section1 = doc1.getSections().get(0);
        //Get header and footer for document 1
        HeaderFooter header = section1.getHeadersFooters().getHeader();
        HeaderFooter footer = section1.getHeadersFooters().getFooter();

        //Load document 2
        Document doc2 = new Document();
        doc2.loadFromFile("target.docx");

        //Traversing document 2's section
        for (int i = 0; i< doc2.getSections().getCount();i++)
        {
            Section section2 = doc2.getSections().get(i);
            //Traverse objects in header
            for(int j = 0 ; j< header.getChildObjects().getCount();j++)
            {
                //Get all subobjects in the header
                Object object1 = header.getChildObjects().get(j);
                //Copy the header of document 1 and add it to document 2
                section2.getHeadersFooters().getHeader().getChildObjects().add(((DocumentObject) object1).deepClone());
            }

            //Copy footer the same way
            for(int z = 0 ; z< footer.getChildObjects().getCount();z++)
            {
                Object object2 = footer.getChildObjects().get(z);
                section2.getHeadersFooters().getFooter().getChildObjects().add(((DocumentObject) object2).deepClone());
            }
        }

        //Save document 2
        doc2.saveToFile("CopyHeaderFooter.docx",FileFormat.Docx_2013);
        doc2.dispose();
    }
}

Header copy effect:

Footer copy effect:

 

[example 3] copy Word watermark

import com.spire.doc.*;

public class CopyWatermark {
    public static void main(String[] args) {
        //Load document 1
        Document doc1 = new Document();
        doc1.loadFromFile("test.docx");

        //Load document 2
        Document doc2 = new Document();
        doc2.loadFromFile("target.docx");

        //Get the watermark effect of document 1 and set it to document 2
        doc2.setWatermark(doc1.getWatermark());

        //Save document 2
        doc2.saveToFile("CopyWatermark.docx",FileFormat.Docx_2013);
        doc2.dispose();
    }
}

Watermark copy effect (this method is applicable to copy text watermark or image watermark):

 

Note: for Word whose document structure is relatively copied, the above methods can be combined to copy and view the copy effect.

 

(end)

Keywords: Java Maven

Added by kayess2004 on Fri, 24 Apr 2020 11:45:34 +0300