I'm trying to merge two docx documents in one.
I've found a lot of forums posts about this in various forums, but none seemed to have been dealing with my problem :
Some of my documents have headers and/or footers while others don't.
As a result, merging the documents can get really messy with headers of one document 'overflowing" on the others.
What I want is to append documents in a new page.
Looking into the resulting document (opened as a zip), modifying it manually to get it right so I would understand what I needed to do programatically, I figured I needed to remove the sectPr from the body of my documents, put it in a P > pPr I would add to the body, and then create a sectPr for my Body with an empty Header and Footer (if I'm wrong, do correct me !)
Anyway, my problem is that, when I programatically create these new Header and Footer, it seems they don't physically exist (i.e. as a File inside the docx archive), wich makes my code crash whenever I want to Save my document to a byte array. I get a "part 'word/header.xml' not found".
Closest issue I could find was this one : docx-java-f6/merge-document-error-t2584.html but the suggested solution didn't seem to solve my issue.
Here's what my code look like (I don't have my code available, so I'm just trying to recreate it from memory)
- Code: Select all
private static void stopHeaderAndFooterOverflow(WordprocessingMLPackage doc) {
SectPr bodySectPr = doc.getMainDocumentPart().getContent().getBody().getSectPr();
if(bodySectPr != null) {
//Sets the (hidden) SectPr of the body inside a paragraph object (results in a section / page break)
P p = factory.createP();
PPr pPr = factory.createPPr();
p.setPPr(pPr);
p.getPPr().setSectPr(bodySectPr);
//Changes the body's SectPr to a clean SectPr with an empty Header and Footer
//First, creates the Header and the Footer programatically
CTRel footer = null, header = null;
footer = factory.createFooterReference();
footer.setType(Type.DEFAULT);
header = factory.createHeaderReference();
header.setType(Type.DEFAULT);
//Empties the header / footer info, but it seems it's not enough : if I don't define a new header / footer, it will overflow from the P > pPr > SectPr we defined above !
bodySectPr.getEgHdrFtrReferences.clear();
//Sets the header / footer info to the ones we programatically created to be empty
bodySectPr.getEgHdrFtrReferences.add(footer);
bodySectPr.getEgHdrFtrReferences.add(header);
}
}
private static void mergeDocuments(WordprocessingMLPackage main, WordprocessingMLPackage chunk) {
//Transforms the docx "chunk" to a byte array so I could inject it in the docx "main"
Save saver = new Save(chunk);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
saver.save(baos); // <<<<<<<<<< Program crashs here
main.getMainDocumentPart().addAltChunk(AltChunkType.WordprocessingML, baos.toByteArray());
}
I hope I could recreate my code as closely as I needed to, if further informations are required, ask away.
Thanks by advance for any help / insight provided !