Im very new to docx4j and i've managed to get a bit working but i am stuck on this.
I have 3 docx files. A header.docx, footer.docx and body.docx.
I load all 3 these files and then try merginf them to create a new file which i'd like to also export to a pdf thereafter.
I have managed to get the new file created and it displays either header or footer with the body in the new file.
Depending on which either header or footer is added to the sectPr that is what displays in the new file but i cannot get both displayed. Also it only displays on the first page even though i have set the type to DEFAULT type in the references.
I might be merging the documents incorrectly which is causing all the problems later on.
Please can someone just have a look at how i create the header/footer parts and references and tell my if it is indeed correct. I would just like the same header and footer to display on all pages after i merged the 3 documents.
Thanks and Regards
Sean
Code below:
//loading the 3 files
WordprocessingMLPackage wordprocessingMLPackageHeader = loadADocXFile(TEMPLATE_FOLDER, HEADER_TEMPLATE_NAME);
WordprocessingMLPackage wordprocessingMLPackageFooter = loadADocXFile(TEMPLATE_FOLDER, FOOTER_TEMPLATE_NAME);
WordprocessingMLPackage wordprocessingMLPackageBody = loadADocXFile(TEMPLATE_FOLDER, BODY_TEMPLATE_NAME);
private WordprocessingMLPackage loadADocXFile(String templateFolder, String templateName) {
WordprocessingMLPackage wordprocessingMLPackage = null;
try {
wordprocessingMLPackage = WordprocessingMLPackage.load(new java.io.File(templateFolder + templateName));
} catch (Docx4JException e) {
System.out.println("* Error occurred whilst loading a DOCX file called : "
+ templateName + " from folder : " + templateFolder);
e.printStackTrace();
}
return wordprocessingMLPackage;
}
// creating header and footer parts and then their references...
Relationship relationshipHeader = createHeaderPart(body, header);
Relationship relationshipFooter = createFooterPart(body, footer);
sectPr = objectFactory.createSectPr();
body = createHeaderReferences(body, relationshipHeader);
body = createFooterReferences(body, relationshipFooter);
body.getMainDocumentPart().addObject(sectPr);
boolean successfullSave = saveTheFile(body);
public static WordprocessingMLPackage createHeaderReferences(WordprocessingMLPackage outputFileMLPackage, Relationship relationshipHeader) throws InvalidFormatException {
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationshipHeader.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
headerReference.setParent(sectPr);
return outputFileMLPackage;
}
public static WordprocessingMLPackage createFooterReferences(WordprocessingMLPackage outputFileMLPackage, Relationship relationshipFooter) throws InvalidFormatException {
FooterReference footerReference = objectFactory.createFooterReference();
footerReference.setId(relationshipFooter.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);
footerReference.setParent(sectPr);
return outputFileMLPackage;
}
public static Relationship createHeaderPart(
WordprocessingMLPackage wordprocessingMLPackage,
WordprocessingMLPackage wordprocessingMLPackageHeader)
throws Exception {
HeaderPart headerPart = new HeaderPart();
headerPart.setPackage(wordprocessingMLPackage);
headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart,
wordprocessingMLPackageHeader));
return wordprocessingMLPackage.getMainDocumentPart().addTargetPart(
headerPart);
}
public static Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
Part sourcePart,
WordprocessingMLPackage wordprocessingMLPackageHeader)
throws Exception {
Hdr hdr = objectFactory.createHdr();
hdr.getEGBlockLevelElts().add(
wordprocessingMLPackageHeader.getMainDocumentPart()
.getJaxbElement().getBody());
return hdr;
}
public static Relationship createFooterPart(
WordprocessingMLPackage wordprocessingMLPackage,
WordprocessingMLPackage wordprocessingMLPackageFooter)
throws Exception {
FooterPart footerPart = new FooterPart();
footerPart.setPackage(wordprocessingMLPackage);
footerPart.setJaxbElement(getFtr(wordprocessingMLPackage, footerPart,
wordprocessingMLPackageFooter));
return wordprocessingMLPackage.getMainDocumentPart().addTargetPart(
footerPart);
}
public static Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage,
Part sourcePart,
WordprocessingMLPackage wordprocessingMLPackageFooter)
throws Exception {
Ftr ftr = objectFactory.createFtr();
ftr.getEGBlockLevelElts().add(
wordprocessingMLPackageFooter.getMainDocumentPart()
.getJaxbElement().getBody());
return ftr;
}