header2.xml //this should be the header for the first page of the document
header1.xml //this should be the header for the rest of the document
Now I'm using this code:
- Code: Select all
WordprocessingMLPackage tempPkg = WordprocessingMLPackage.load(new File(templateFile));
List<SectionWrapper> sectionWrappers = tempPkg.getDocumentModel().getSections();
System.out.println("Sections\n");
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
System.out.println("Header");
if (hfp.getFirstHeader()!=null) {
System.out.println("-first");
System.out.println(hfp.getFirstHeader().getPartName());
Part headerPart = hfp.getFirstHeader(); //header 2 for first page
headerPart.setPartName(new PartName("/word/header2.xml"));
//System.out.println("header2.xml:" + headerPart.getPartName() + " " + headerPart);
Relationship rh1 = mainDocumentPart.addTargetPart(headerPart);
createHeaderReference(wordMLPackage, rh1,1);
}
if (hfp.getDefaultHeader()!=null){
System.out.println("-default");
System.out.println(hfp.getFirstHeader().getPartName());
Part headerPart = hfp.getDefaultHeader();
headerPart.setPartName(new PartName("/word/header1.xml"));
//header 1 should be the default header
//System.out.println("header1.xml:" + headerPart.getPartName() + " " + headerPart);
Relationship rh2 = mainDocumentPart.addTargetPart(headerPart);
createHeaderReference(wordMLPackage, rh2,3);
}
}
First of all i noticed that i get the following output on the console:
Sections
Header
-first
/word/header2.xml
-default
/word/header2.xml
But where is the header1.xml ??
And moreover this
Part headerPart = hfp.getFirstHeader(); //header 2 for first page
headerPart.setPartName(new PartName("/word/header2.xml"));
System.out.println("header2.xml:" + headerPart.getPartName() + " " + headerPart);
Relationship rh1 = mainDocumentPart.addTargetPart(headerPart);
createHeaderReference(wordMLPackage, rh1,1);
didn't work. Im getting an error:
- Code: Select all
org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:164)
at de.fhzw.portal.studienverzeichnis.util.print.WordDocument.flush(WordDocument.java:198)
at de.fhzw.portal.studienverzeichnis.util.print.ModulhandbuchDocxUtil.generateModulhandbuch(ModulhandbuchDocxUtil.java:135)
at de.fhzw.portal.studienverzeichnis.print.RTFServlet.service(RTFServlet.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:428)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 21 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:428)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 23 more
Caused by: java.lang.NullPointerException
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:397)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 25 more
102109 ERROR- de.fhzw.portal.studienverzeichnis.util.print.WordDocument - flush - Docx4JException, konnte Dokument nicht in outputStream schreiben.
org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:164)
at de.fhzw.portal.studienverzeichnis.util.print.WordDocument.flush(WordDocument.java:198)
at de.fhzw.portal.studienverzeichnis.util.print.ModulhandbuchDocxUtil.generateModulhandbuch(ModulhandbuchDocxUtil.java:135)
at de.fhzw.portal.studienverzeichnis.print.RTFServlet.service(RTFServlet.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:428)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 21 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:378)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:428)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 23 more
Caused by: java.lang.NullPointerException
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:397)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:373)
... 25 more
I'm working very similar with the footer and that causes no error:
- Code: Select all
RelationshipsPart rp = tempPkg.getMainDocumentPart().getRelationshipsPart();
Relationship rel = rp.getRelationshipByType(Namespaces.FOOTER);
Part footerPart = rp.getPart(rel);
footerPart.setPartName(new PartName("/word/footer1.xml"));
//Hinzufuegen
Relationship rFooter = mainDocumentPart.addTargetPart(footerPart);
createFooterReference(wordMLPackage, rFooter);
Moreover I think the header I'm trying to add is not null ...
I have tried various types of adding the header but nothing worked well (only adding an empty one, but that is not what I want). Is it a problem if there is a picture in the header? I'm desperate and have no more ideas at the moment...
Maybe further helpfull code:
- Code: Select all
private void createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship relationship , int type)
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();
mainDocumentPart.addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference reference = objectFactory.createHeaderReference();
reference.setId(relationship.getId());
switch(type){
case 1:
reference.setType(HdrFtrRef.FIRST);
break;
case 2:
reference.setType(HdrFtrRef.EVEN);
break;
default:
reference.setType(HdrFtrRef.DEFAULT);
break;
}
sectPr.getEGHdrFtrReferences().add(reference);
}
I would be grateful if someone could help me with this.
Best regards
Sandra