Page 1 of 1

Only header or footer displayed on page not both

PostPosted: Thu Jul 08, 2010 10:35 pm
by sparkynarf2003
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;

}

Re: Only header or footer displayed on page not both

PostPosted: Fri Jul 09, 2010 10:50 am
by jason
Hi Sean

I looked at the code you sent me by email.

Everything works if you use:
Code: Select all
                hdr.getEGBlockLevelElts().addAll(
                      wordprocessingMLPackageHeader.getMainDocumentPart().getJaxbElement().getBody().getEGBlockLevelElts());


(and the equivalent for the footer). You want to add the children of the body element, not the body element itself (headers/footers don't have that).

I haven't looked at the code in this forum post.

I suggest you generate your PDF via XSL FO. The iText approach will be removed in a later release of docx4j.

Also, please drop in http://dev.plutext.org/docx4j/docx4j-ni ... 100708.jar

That supports different headers/footers in each section in the PDF output (via XSL FO).

cheers .. Jason

Re: Only header or footer displayed on page not both

PostPosted: Fri Jul 09, 2010 5:49 pm
by jason
You can use docx4j-2.4.0, which I released today. See the release sticky above for details.

Re: Only header or footer displayed on page not both

PostPosted: Fri Jul 09, 2010 9:25 pm
by sparkynarf2003
Hi Jason,

Thanks for all the help i really appreciate it!

I'm using docx4j-2.4.0.jar now and I also had to move from using JAXB2.0 to JAXB2.1 when i wanted to generate PDF's viaXSLFO.

Changing my code from: ftr.getEGBlockLevelElts().add(wordprocessingMLPackageFooter.getMainDocumentPart().getJaxbElement().getBody());
to: ftr.getEGBlockLevelElts().addAll(wordprocessingMLPackageFooter.getMainDocumentPart().getJaxbElement().getBody().getEGBlockLevelElts()); worked immediately.

Header and Footer is now displayed on all pages! :D

Regards
Sean