Merging MS Word documents with Java

docx4j merge documents
apache poi-mail merge

I'm looking for java libraries that read and write MS Word Document. What I have to do is:

  • read a template file, .dot or .doc, and fill it with some data read from DB
  • take data from another Word document and merging that with the file described above, preserving paragraphs formats

users may make updates to the file.

I've searched and found POI Apache and UNO OpenOffice. The first one can easily read a template and replace any placeholders with my own data from DB. I didn't found anything about merging two, or more, documents. OpenOffice UNO looks more stable but complex too. Furthermore I'm not sure that it has the ability to merge documents..

We are looking the right direction?

Another solution i've thought was to convert doc file to docx. In that way I found more libraries that can help us merging documents. But how can I do that?


You could take a look at Docmosis since it provides the four features you have mentioned (data population, template/document merging, DOC format and java interface). It has a couple of flavours (download, online service), but you could sign up for a free trial of the cloud service to see if Docmosis can do what you want (then you don't have to install anything) or read the online documentation.

It uses OpenOffice under the hood (you can see from the developer guide installation instructions) which does pretty decent conversions between documents. The UNO API has some complications - I would suggest either Docmosis or JODReports to isolate your project from UNO directly.

Hope that helps.

Merging MS Word documents with Java, You could take a look at Docmosis since it provides the four features you have mentioned (data population, template/document merging, DOC format and java  Mail merge in java for Microsoft Word document – Part I. In one of the projects I was working on – we defined template using XSL, passed XML data and then generated PDF using Apache FOP. But it was tough to manage change – for even changing a single dot, the business needed to contact the developer.

import java.util.List;

import javax.xml.bind.JAXBException;

import org.docx4j.dml.CTBlip;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.relationships.Relationship;

public class MultipleDocMerge {

    public static void main(String[] args) throws Docx4JException, JAXBException {
        File first = new File("D:\\Mreg.docx");
        File second = new File("D:\\Mreg1.docx");
        File third = new File("D:\\Mreg4&19.docx");
        File fourth = new File("D:\\test12.docx");   
        WordprocessingMLPackage f = WordprocessingMLPackage.load(first);
        WordprocessingMLPackage s = WordprocessingMLPackage.load(second);
        WordprocessingMLPackage a = WordprocessingMLPackage.load(third);
        WordprocessingMLPackage e = WordprocessingMLPackage.load(fourth);

        List body = s.getMainDocumentPart().getJAXBNodesViaXPath("//w:body", false);
        for(Object b : body){
            List filhos = ((org.docx4j.wml.Body)b).getContent();
            for(Object k : filhos)

        List body1 = a.getMainDocumentPart().getJAXBNodesViaXPath("//w:body", false);
        for(Object b : body1){
            List filhos = ((org.docx4j.wml.Body)b).getContent();
            for(Object k : filhos)

        List body2 = e.getMainDocumentPart().getJAXBNodesViaXPath("//w:body", false);
        for(Object b : body2){
            List filhos = ((org.docx4j.wml.Body)b).getContent();
            for(Object k : filhos)

        List<Object> blips = e.getMainDocumentPart().getJAXBNodesViaXPath("//a:blip", false);
        for(Object el : blips){
            try {

                   CTBlip blip = (CTBlip) el;
                   RelationshipsPart parts = e.getMainDocumentPart().getRelationshipsPart();
                   Relationship rel = parts.getRelationshipByID(blip.getEmbed());
                   Part part = parts.getPart(rel);
                   if(part instanceof ImagePngPart)
                        System.out.println(((ImagePngPart) part).getBytes()); 
                   if(part instanceof ImageJpegPart)
                        System.out.println(((ImageJpegPart) part).getBytes()); 
                    if(part instanceof ImageBmpPart)
                        System.out.println(((ImageBmpPart) part).getBytes()); 
                    if(part instanceof ImageGifPart)
                        System.out.println(((ImageGifPart) part).getBytes()); 
                    if(part instanceof ImageEpsPart)
                        System.out.println(((ImageEpsPart) part).getBytes()); 
                    if(part instanceof ImageTiffPart)
                        System.out.println(((ImageTiffPart) part).getBytes()); 
                    Relationship newrel = f.getMainDocumentPart().addTargetPart(part,AddPartBehaviour.RENAME_IF_NAME_EXISTS);
                    f.getMainDocumentPart().addTargetPart(e.getParts().getParts().get(new PartName("/word/"+rel.getTarget())));
                } catch (Exception ex){
                } }

        File saved = new File("D:\\saved1.docx");;



MergeDocx - Java, Merging Word documents in Java. This page is about programmatically merging Word documents in Java. Merge as in concatenate/join/append, not  1) ReadFile Class: Reads the text file and returns list of values. These values will be our parameters for word generation. 2) WordGenerator Class: It contains getLines and createWord methods. First we get the values with getLines method and createWord method to create word documents.

I've developed the next class (using Apache POI):

import java.util.ArrayList;
import java.util.List;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

public class WordMerge {

    private final OutputStream result;
    private final List<InputStream> inputs;
    private XWPFDocument first;

    public WordMerge(OutputStream result) {
        this.result = result;
        inputs = new ArrayList<>();

    public void add(InputStream stream) throws Exception{            
        OPCPackage srcPackage =;
        XWPFDocument src1Document = new XWPFDocument(srcPackage);         
        if(inputs.size() == 1){
            first = src1Document;
        } else {            
            CTBody srcBody = src1Document.getDocument().getBody();

    public void doMerge() throws Exception{

    public void close() throws Exception{
        for (InputStream input : inputs) {

And its use:

public static void main(String[] args) throws Exception {

    FileOutputStream faos = new FileOutputStream("/home/victor/result.docx");

    WordMerge wm = new WordMerge(faos);

    wm.add( new FileInputStream("/home/victor/001.docx") );
    wm.add( new FileInputStream("/home/victor/002.docx") );



How to Perform a Mail Merge in a Word Document in Java, In this article, I am going to introduce how to perform a mail merge in Microsoft Word documents using Java. This article uses Free Spire.Doc for  Create a Word document and open it. Click where you want to add merge fields. Open the Insert menu, click Quick Parts and in the drop-down list select Field… to open the Field dialog. In the Field names list, select MergeField.

The Apache POI code does not work for Images.

Java Document Merging API, Documents merging API for Java. Merge, split, swap, reorder and delete pages of PDF, Microsoft Word, Excel, presentations, Visio, XPS & EPUB formats. Working with MS Word documents is very hard. DOC format is very complicated, DOCX is in theory simpler, but it is rather new and there is no mature, feature rich Java library to work with it and DOC as well. The easiest solutions to most problems connected with working with MS Office documents is describe in the Joel Spolsky article. I hope you can use this method in your application.

How to do Mail Merge from Java to PDF, Doc, DocX and more., Java Mail Merge Library. Create templates in Microsoft Word or OpenOffice. Merge with your data to create PDF, Doc, DocX, ODT, RTF or HTML. Re: compare and merge 2 ms word docx or doc. by jason » Mon May 18, 2009 8:08 am ok first, docx4j deals mainly with docx documents, so if one of the documents you want to compare is an old binary .doc, you will need to convert it to docx first.

Merge Documents - Aspose.Words for Java, Words Java for docx4j · Code Comparison for Common Features in You can try to merge documents and view the results online at this link: @see http://www.​ for more  The Insert tab allows you to subtly merge documents. Locate Object , press a small triangle next to it, and click Text from File from the dropdown menu. After that, you can select files to be merged into the current document.

docx4java aka docx4j – OpenXML office documents in Java » Blog , Merging Word documents. I've written a utility to merge docx documents in Java. “​Merge” as in concatenate/join/append, as opposed to  The Combine Documents dialog should now appear, overlaying your main Word window. Select the main document, either from the drop-down menu or by selecting the folder icon found within the Original document section. Repeat this step within the Revised document section, replacing the main document with the file containing the changes that you wish to merge.

  • thanks, but the requiriment was .doc documents!
  • What do you mean by "merging" documents? Just appending the content of one to another?
  • Exactly. Typically append content to the end
  • Use JODConverter to convert doc to docx.
  • Try to add an explanation also.
  • It is not working in case of images.
  • Now, what is your solution?