I used the demo webapp to show the code required for my document. I am substituting in values from a data model and building the 4 different types of pages I need as I go.
But I cannot get the 3 headers (and 3 footers) from the demo webapp to display in the Word document produced.
I have this code to call my header and footer create methods:
- Code: Select all
public Body createIt(Body body) {
// Create object for sectPr
SectPr sectpr = wmlObjectFactory.createSectPr();
body.setSectPr(sectpr);
try {
createHeader(1);
createHeader(2);
createHeader(3);
createFooter(1);
createFooter(2);
createFooter(3);
} catch (IOException ioe) {
} catch (Exception e) {
}
The header and footer create methods are much the same so I'll only show the header code here.
- Code: Select all
public void createHeader(int version) throws IOException, Exception {
// Create object for headerReference1
HeaderPart headerPart = null;
Relationship rel = null;
try {
headerPart = new HeaderPart();
rel = titlePage.wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
// After addTargetPart, so image can be added properly
Hdr hdr = null;
if (version == 1) {
Header1 hdr1 = new Header1();
hdr = hdr1.createIt(wmlObjectFactory);
} else if (version == 2) {
Header2 hdr2 = new Header2();
hdr = hdr2.createIt(wmlObjectFactory);
} else if (version == 3) {
Header3 hdr3 = new Header3();
hdr = hdr3.createIt(wmlObjectFactory);
}
java.io.InputStream is = TitlePage.class.getResourceAsStream("/headerimg.png");;
hdr.getContent().add(
newImage(titlePage.wordMLPackage,
headerPart,
BufferUtil.getBytesFromInputStream(is),
"filename", "alttext", 1, 2
)
);
headerPart.setJaxbElement(hdr);
List<SectionWrapper> sections = titlePage.wordMLPackage.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr==null ) {
sectPr = wmlObjectFactory.createSectPr();
titlePage.wordMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = wmlObjectFactory.createHeaderReference();
headerReference.setId(rel.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The 3 Header1..3 classes are as generated by the demo webapp for the headers.
I trace thru debug and see there is only one header relationship in
- Code: Select all
titlePage.wordMLPackage.getMainDocumentPart().realtionships.jaxbElement.relationship
Thank you