So here it goes..
- Code: Select all
public static void createHeaderPart(WordprocessingMLPackage wordprocessingMLPackage)
throws Exception {
MainDocumentPart mainDocumentPart = wordprocessingMLPackage.getMainDocumentPart();
HeaderPart headerPart1 = new HeaderPart();
headerPart1.setPackage(wordprocessingMLPackage);
Hdr hdr1 = getHdr(true);
headerPart1.setJaxbElement(hdr1);
Relationship r1 = mainDocumentPart.addTargetPart(headerPart1, "rIdmy1");
// r1.setTarget("header1.xml");
// This one causes runtime error
HeaderPart headerPart2 = new HeaderPart();
headerPart2.setPackage(wordprocessingMLPackage);
Hdr hdr2 = getHdr(false);
headerPart2.setJaxbElement(hdr2);
Relationship r2 = mainDocumentPart.addTargetPart(headerPart2, "rIdmy2");
// r2.setTarget("header2.xml");
// as before
List<SectionWrapper> sections = wordprocessingMLPackage.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 = objectFactory.createSectPr();
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference1 = new HeaderReference();
headerReference1.setId(r1.getId());
headerReference1.setType(HdrFtrRef.FIRST);
HeaderReference headerReference2 = new HeaderReference();
headerReference2.setId(r2.getId());
headerReference2.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference1);
sectPr.getEGHdrFtrReferences().add(headerReference2);
}
public static Hdr getHdr(boolean first) throws Exception {
String hdrXml = new String();
String hdrXml1 = "bla bla 1... valid xml for header";
String hdrXml2 = "bla bla 2... other valid xml for header";
if (first) {
hdrXml = hdrXml1;
}
else {
hdrXml = hdrXml2;
}
Hdr hdr = objectFactory.createHdr();
hdr = (Hdr)XmlUtils.unmarshalString(hdrXml);
return hdr;
}
The document that gets created, only contains "bla bla 2" in its header, though the purpose is that "bla bla 1" should be in the header of the first page. It seems like the code for adding header info for first page gets omitted somehow. Am I missing something obvious here???
Any help will be greatly appreciated!