Problem merging two docx files
Posted: Wed Apr 01, 2015 7:56 pm
Hello,
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:
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,
Harald
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.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.utils.SingleTraversalUtilVisitorCallback;
import org.docx4j.wml.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
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");
return;
}
int totalsize = documentPart.getJaxbElement().getBody().getContent().size();
blockRanges.add(new BlockRange(wordMLPackage, 0, n));
BlockRange fb = new BlockRange(WordprocessingMLPackage.load(new File(input_FB)));
fb.setFooterBehaviour(BlockRange.HfBehaviour.INHERIT);
fb.setHeaderBehaviour(BlockRange.HfBehaviour.INHERIT);
// No pages breaks
fb.setSectionBreakBefore(BlockRange.SectionBreakBefore.CONTINUOUS);
blockRanges.add(fb);
blockRanges.add(new BlockRange(wordMLPackage, n+1, totalsize));
DocumentBuilder documentBuilder = new DocumentBuilder();
WordprocessingMLPackage output = documentBuilder.buildOpenDocument(blockRanges);
output.save(new 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;
}
}
}
}
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,
Harald