Unfortunately, I do have a bigger problem involving headers. I'm trying to put a simple table within the header part of the document and am having a ton of trouble (aka no success) getting it done. I've read the other forum threads regarding 2 footers, and editing images within headers, and also have taken a look and tried to use parts of the HeaderFooterCreate.java example that you have on the site. Still haven't had any success though. Essentially all I want to do is create the header part (or access it if it created by default, which I don't think is the case) and then add a table object to it. I assumed this would be simple (still might be) but I haven't been able to figure it out.
Here is the snippet of the code that attempts to add my table (headerTbl) to the headerPart that i've created. I'm hoping that i'm only missing one or two key components, but perhaps i'm way off.
Using java Syntax Highlighting
HeaderPart header = new HeaderPart(new PartName("/word/header.xml"));
Hdr hdr = objectFactory.createHdr();
hdr.getContent().add(headerTbl);
header.setJaxbElement(hdr);
Relationship rel1 = wordMLPackage.getMainDocumentPart().addTargetPart(header);
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(rel1.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
public static void createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship relationship )
throws InvalidFormatException {
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 headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
Hdr hdr = objectFactory.createHdr();
hdr.getContent().add(headerTbl);
header.setJaxbElement(hdr);
Relationship rel1 = wordMLPackage.getMainDocumentPart().addTargetPart(header);
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(rel1.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
public static void createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship relationship )
throws InvalidFormatException {
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 headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
Parsed in 0.017 seconds, using GeSHi 1.0.8.4
Thanks again, Jason