Page 1 of 1

Problem merging two docx files

PostPosted: Wed Apr 01, 2015 7:56 pm
by HarryVienna

I just tried the enterprise extension to find out if we can use it for our project.

I wanted to merge two docx files, but the output file is courrupt. I found out, that the reason is a footnote in the docx file, that should be included in the master docx file. As soon as I remove the footnote, it works.

Here is my code:

Code: Select all
package at.rcm.docx4j;

import com.plutext.merge.BlockRange;
import com.plutext.merge.DocumentBuilder;
import org.docx4j.Docx4J;
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.utils.SingleTraversalUtilVisitorCallback;
import org.docx4j.wml.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class mergeRB {

    private static WordprocessingMLPackage  wordMLPackage;
    private static ObjectFactory objFactory;
    private static Logger log = LoggerFactory.getLogger(mergeRB.class);

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

        objFactory = Context.getWmlObjectFactory();

        // the docx 'template'
        String input_DOCX = System.getProperty("user.dir") + "/data/rb3.docx";

        String input_FB = System.getProperty("user.dir") + "/data/FB_EPR.docx";

        // Load input_template.docx
        wordMLPackage = Docx4J.load(new File(input_DOCX));

        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
        Document wmlDocumentEl = wordMLPackage.getMainDocumentPart().getContents();

        List<BlockRange> blockRanges = new ArrayList<BlockRange>();

        int n = getIndexOfSdt(documentPart, "Fondsbestimmungen");
        if (n==-1) {
            System.out.println("Couldn't find index");
        int totalsize = documentPart.getJaxbElement().getBody().getContent().size();

        blockRanges.add(new BlockRange(wordMLPackage, 0, n));

        BlockRange fb = new BlockRange(WordprocessingMLPackage.load(new File(input_FB)));
        // No pages breaks

        blockRanges.add(new BlockRange(wordMLPackage, n+1, totalsize));

        DocumentBuilder documentBuilder = new DocumentBuilder();
        WordprocessingMLPackage output = documentBuilder.buildOpenDocument(blockRanges); File(System.getProperty("user.dir") + "/data/rb3_out.docx") );


    public static int getIndexOfSdt(MainDocumentPart documentPart, String alias) {

        int index = 0;
        List<Object> all = documentPart.getJaxbElement().getBody().getContent();
        for (Object o : all ) {

            if (o instanceof SdtBlock) {
                SdtPr sdtPr= ((SdtBlock)o).getSdtPr();
                if (sdtPr!=null) {
                    for (Object pr : sdtPr.getRPrOrAliasOrLock() ) {
                        // Annoyingly, it may be wrapped in javax.xml.bind.JAXBElement
                        pr = XmlUtils.unwrap(pr);
                        if (pr instanceof SdtPr.Alias && ((SdtPr.Alias)pr).getVal().equals(alias)) {
                            return index;

        return -1;


I also attached the two files. Is this my fault or is there a problem with footnotes? Would be great if you can help me because the decision to buy the extension depends on that feature.

Thanks very much,

Re: Problem merging two docx files

PostPosted: Wed Apr 01, 2015 10:17 pm
by jason
Hi Harald

footnotes should be fine.

I'll have a look at your documents tomorrow and let you know.

cheers .. Jason

Re: Problem merging two docx files

PostPosted: Wed Apr 01, 2015 11:17 pm
by HarryVienna
Thanks Jason, that would be great. Just for info, I use docx4j 3.2.1 and docx4j-enterprise


Re: Problem merging two docx files

PostPosted: Thu Apr 02, 2015 3:27 pm
by jason
Hello again Harald

The issue was caused by the presence of a hyperlink in a footnote.

It is now fixed in

To get it, please download the new trial version, from ...

cheers .. Jason

Re: Problem merging two docx files

PostPosted: Thu Apr 02, 2015 6:08 pm
by HarryVienna
Thank you very much, works great!!