I want to create two headers and two footers in a word document, for example in the attached word file.
I tried for a long time but not found a solution.
Can someone kindly give me some help (some code better), thanks a lot!
public static void addFooter(WordprocessingMLPackage wordMLPackage) throws InvalidFormatException, JAXBException {
FooterPart footerPart = new FooterPart();
Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);
String ftrXml = "<w:ftr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
+ "<w:p w:rsidR=\"00CD3E70\" w:rsidRDefault=\"00ED630A\" w:rsidP=\"00AB75CC\">"
+ "<w:pPr><w:tabs><w:tab w:val=\"right\" w:pos=\"9360\"/></w:tabs><w:rPr><w:lang w:eastAsia=\"zh-CN\"/></w:rPr></w:pPr>"
+ "<w:r><w:t>111</w:t></w:r>"
+ "<w:r w:rsidR=\"00AB75CC\"><w:tab/></w:r>"
+ "<w:r w:rsidR=\"00AB75CC\"><w:rPr><w:rFonts w:hint=\"eastAsia\"/><w:lang w:eastAsia=\"zh-CN\"/></w:rPr><w:t>222</w:t></w:r>"
+ "</w:p>"
+ "</w:ftr>";
Ftr ftr = (Ftr) XmlUtils.unmarshalString(ftrXml);
footerPart.setJaxbElement(ftr);
// Now Create foooter reference
ObjectFactory objectFactory = new ObjectFactory();
List<SectionWrapper> sections = 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 = objectFactory.createSectPr();
wordMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
FooterReference footerReference = objectFactory.createFooterReference();
footerReference.setId(rel.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);
}
zhj89901390 wrote:can you give me some detailed code? thanks a lot!
Return to content controls (w:sdt) and data binding
Users browsing this forum: No registered users and 7 guests