Page 1 of 1

Merge header (text and image) and body into new docx file

PostPosted: Sat Jul 10, 2010 1:16 am
by sparkynarf2003
I have managed to merge 3 docx files into one. So in other words a header.docx, footer.docx and body.docx into a new docx file. I then managed to also substitute mail merge placeholder using ${} with values and finally converted the merged and populated docx file into a pdf using the viaXSLFO method and docx4j-2.4.0.jar with JAXB 2.1 and implementation jar. I can however not get it right to merge a header.docx that contains an image like a logo as well as text. My code is below and i have attached the inputted and outputted files for reference.

Please can someone point me in the right direction concerning taking the header.docx (that has text and image) and merging it into the newly created docx? I cannot find anything like that on the forum only creating from filesystem i think.

Thanks in advance
Sean

Code:

HeaderPart headerPart = new HeaderPart();
HeaderPart.setPackage(wordprocessingMLPackageBody);
Hdr hdr = objectFactory.createHdr();
hdr.getEGBlockLevelElts().addAll(wordprocessingMLPackageHeader.getMainDocumentPart().getJaxbElement().getBody().getEGBlockLevelElts());
headerPart.setJaxbElement(hdr);
Relationship relationshipHeader = wordprocessingMLPackageBody.getMainDocumentPart().addTargetPart(headerPart);
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationshipHeader.getId());
headerReference.setType(HdrFtrRef.DEFAULT);

SectPr sectPr = objectFactory.createSectPr();
sectPr.getEGHdrFtrReferences().add(headerReference);
sectPr.getEGHdrFtrReferences().add(footerReference);
wordprocessingMLPackageBody.getMainDocumentPart().getJaxbElement().getBody().setSectPr(sectPr);