That's the code that I use to generate the footer
- Code: Select all
private void addPageNumber(WordprocessingMLPackage wordMLPackage)
throws Exception {
// Delete the Styles part, since it clutters up our output
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
Relationship styleRel = mdp.getStyleDefinitionsPart()
.getSourceRelationships().get(0);
mdp.getRelationshipsPart().removeRelationship(styleRel);
// OK, the guts of this sample:
// The 2 things you need:
// 1. the Header part
Relationship relationship = createFooterPart(wordMLPackage);
// 2. an entry in SectPr
createFooterReference(wordMLPackage, relationship);
}
public Relationship createFooterPart(
WordprocessingMLPackage wordprocessingMLPackage) throws Exception {
FooterPart footerPart = new FooterPart();
Relationship rel = wordprocessingMLPackage.getMainDocumentPart()
.addTargetPart(footerPart);
// After addTargetPart, so image can be added properly
footerPart.setJaxbElement(getFtr(wordprocessingMLPackage));
return rel;
}
public void createFooterReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship relationship) throws InvalidFormatException {
ObjectFactory objectFactory = Context.getWmlObjectFactory();
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);
}
FooterReference footerReference = objectFactory.createFooterReference();
footerReference.setId(relationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);// add header or
// footer references
}
public Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage)
throws Exception {
ObjectFactory factory = Context.getWmlObjectFactory();
Ftr ftr = factory.createFtr();
P paragrafo = factory.createP();
PPr ppr = factory.createPPr();
PStyle pStyle = new PStyle();
pStyle.setVal("Footer");
ppr.setPStyle(pStyle);
CTFramePr ctFramePr = factory.createCTFramePr();
ctFramePr.setWrap(STWrap.AROUND);
ctFramePr.setVAnchor(STVAnchor.TEXT);
ctFramePr.setHAnchor(STHAnchor.MARGIN);
ctFramePr.setXAlign(STXAlign.RIGHT);
ctFramePr.setY(BigInteger.ONE);
ppr.setFramePr(ctFramePr);
ParaRPr rPrPageNumber = factory.createParaRPr();
RStyle rStyle = factory.createRStyle();
rStyle.setVal("PageNumber");
rPrPageNumber.setRStyle(rStyle);
ppr.setRPr(rPrPageNumber);
paragrafo.setPPr(ppr);
R r = factory.createR();
RPr rPrNumber = factory.createRPr();
rPrNumber.setRStyle(rStyle);
r.setRPr(rPrNumber);
FldChar fldCharBegin = factory.createFldChar();
fldCharBegin.setFldCharType(STFldCharType.BEGIN);
r.getContent().add(fldCharBegin);
paragrafo.getContent().add(r);
R r2 = factory.createR();
r2.setRPr(rPrNumber);
Text text = factory.createText();
JAXBElement<org.docx4j.wml.Text> textWrapped = factory
.createRInstrText(text);
text.setValue("PAGE");
r2.getContent().add(textWrapped);
paragrafo.getContent().add(r2);
R r3 = factory.createR();
r3.setRPr(rPrNumber);
FldChar fldCharEnd = factory.createFldChar();
fldCharEnd.setFldCharType(STFldCharType.END);
r3.getContent().add(fldCharEnd);
paragrafo.getContent().add(r3);
ftr.getContent().add(paragrafo);
return ftr;
}
when I put a simple text it's appear in pdf, but when I try to put the code to show the page number, doesn't work.
that's the xml generated in footer.
- Code: Select all
<w:p>
<w:pPr>
<w:pStyle w:val="Footer"/>
<w:framePr w:wrap="around" w:hAnchor="margin" w:vAnchor="text" w:xAlign="right" w:y="1"/>
<w:rPr>
<w:rStyle w:val="PageNumber"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rStyle w:val="PageNumber"/>
</w:rPr>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:rPr>
<w:rStyle w:val="PageNumber"/>
</w:rPr>
<w:instrText>PAGE</w:instrText>
</w:r>
<w:r>
<w:rPr>
<w:rStyle w:val="PageNumber"/>
</w:rPr>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
The docx generated is attached