What I have done in the code is to first remove any existing header references from the target document, and then create a new HeaderPart object, set the JaxB element to the one associated with the template's header, and finally add the default header references from the template; The code is as seen below:
- Code: Select all
public void headerMethod() throws InvalidFormatException {
String templatePath = "..\\ComplexHeader.dotx";
WordprocessingMLPackage wordMLPackage = getMainDoc(templatePath);
String existingFileName = "..\\NewEmptyDoc.docx";
WordprocessingMLPackage wordMLPackage1 = getMainDoc(existingFileName);
MainDocumentPart mainDocumentPart1 = wordMLPackage1.getMainDocumentPart();
//removing header references of existing doc
removeHeaderFooterReferences(wordMLPackage1);
List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
for(SectionWrapper sw : sections){
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
HeaderPart header = hfp.getDefaultHeader();
Hdr hdr = header.getJaxbElement();
HeaderPart newHeader = new HeaderPart();
newHeader.setJaxbElement(hdr);
newHeader.setPackage(wordMLPackage1);
Relationship relShip = null;
relShip = mainDocumentPart1.addTargetPart(newHeader);
createHeaderFooterReference(wordMLPackage1,relShip);
}
SaveToZipFile saver = new SaveToZipFile(wordMLPackage1);
try {
saver.save(existingFileName);
} catch (Docx4JException e) {
System.out.println(e.getMessage());
}
}
The removal of header references is done as follows:
- Code: Select all
List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
HeaderPart header = null;
for (SectionWrapper sw : sections) {
header = sw.getHeaderFooterPolicy().getDefaultHeader();
if(header == null) continue;
removeRelationships(header);
wordprocessingMLPackage.getMainDocumentPart().getRelationshipsPart().removeRelationship(header.getPartName());
List<CTRel> rel =sw.getSectPr().getEGHdrFtrReferences();
Iterator<CTRel> relIter = rel.iterator();
while(relIter.hasNext()){
CTRel ctRel = relIter.next();
if (ctRel instanceof HeaderReference) {
HeaderReference hr = (HeaderReference) ctRel;
if (hr.getType().equals(HdrFtrRef.DEFAULT)) {
//sw.getSectPr().getEGHdrFtrReferences().remove(hr);
relIter.remove();
}
}
}
}
The creation of the new header references is done in the same method as the sample code for header/footer creation:
- Code: Select all
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 = objFac.createSectPr();
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = objFac.createHeaderReference();
headerReference.setId(headerRelationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
//FooterReference footerReference = objFac.createFooterReference();
//footerReference.setId(footerRelationship.getId());
//footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
Note: On examining the header xml files for the target document, I had seen that there were a whole set of namespaces associated that were not seen in the header xml files of the template. Could this be controlled, and does this affect the interpretation of the xml file by Microsoft Word when opening the document?
Thanks in advance!