5

Just started to use POI 3.10 to create a Word document (XWPF). Most of the things are straight forward, but I don't understand how to add page numbers. I added the footer, but the text in the footer is the same on every page

4 Answers4

8

I created a page number in the footer on the right in LibreOffice and investigated the XML file (MS Word-Std-Objects are not supported in POI which is used there for page numbers).

This will enable to create more complex footers...

to set the number to other positions set another value for ctjc instead of STJc.RIGHT...

The result is the following:

// create footer
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
CTP ctpFooter = CTP.Factory.newInstance();

XWPFParagraph[] parsFooter;

// add style (s.th.)
CTPPr ctppr = ctpFooter.addNewPPr();
CTString pst = ctppr.addNewPStyle();
pst.setVal("style21");
CTJc ctjc = ctppr.addNewJc();
ctjc.setVal(STJc.RIGHT);
ctppr.addNewRPr();

// Add in word "Page "   
CTR ctr = ctpFooter.addNewR();
CTText t = ctr.addNewT();
t.setStringValue("Page ");
t.setSpace(Space.PRESERVE);

// add everything from the footerXXX.xml you need
ctr = ctpFooter.addNewR();
ctr.addNewRPr();
CTFldChar fch = ctr.addNewFldChar();
fch.setFldCharType(STFldCharType.BEGIN);

ctr = ctpFooter.addNewR();
ctr.addNewInstrText().setStringValue(" PAGE ");

ctpFooter.addNewR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);

ctpFooter.addNewR().addNewT().setStringValue("1");

ctpFooter.addNewR().addNewFldChar().setFldCharType(STFldCharType.END);

XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, doc);

parsFooter = new XWPFParagraph[1];

parsFooter[0] = footerParagraph;

policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
jmarkmurphy
  • 11,030
  • 31
  • 59
Sempfer
  • 171
  • 1
  • 4
3

org.apache.poi:poi:3.17, org.apache.poi:poi-ooxml:3.17, org.apache.poi:ooxml-schemas:1.3

CTP ctp = CTP.Factory.newInstance();

CTText txt = ctp.addNewR().addNewT();
txt.setStringValue("Page ");
txt.setSpace(SpaceAttribute.Space.PRESERVE);

CTR run = ctp.addNewR();
run.addNewFldChar().setFldCharType(STFldCharType.BEGIN);
run.addNewInstrText().setStringValue(" PAGE ");
run.addNewFldChar().setFldCharType(STFldCharType.END);

txt = ctp.addNewR().addNewT();
txt.setStringValue(" of ");
txt.setSpace(SpaceAttribute.Space.PRESERVE);

run = ctp.addNewR();
run.addNewFldChar().setFldCharType(STFldCharType.BEGIN);
run.addNewInstrText().setStringValue(" NUMPAGES ");
run.addNewFldChar().setFldCharType(STFldCharType.END);

XWPFParagraph par = new XWPFParagraph(ctp, document);
par.setAlignment(ParagraphAlignment.CENTER);

XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy();
if (policy == null)
  policy = document.createHeaderFooterPolicy();
policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, new XWPFParagraph[] { par });

And you got Page 1 of 9 in footer.

Grigory K
  • 1,301
  • 10
  • 10
0
CTP ctp = CTP.Factory.newInstance();
//this add page number incremental
ctp.addNewR().addNewPgNum();

XWPFParagraph codePara = new XWPFParagraph(ctp, document);
XWPFParagraph[] paragraphs = new XWPFParagraph[1];
paragraphs[0] = codePara;
//position of number
codePara.setAlignment(ParagraphAlignment.CENTER);

CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();

try {
    XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
    headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, paragraphs);
} catch (IOException | XmlException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
lshared
  • 154
  • 9
  • headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, paragraphs); this line can be: headerFooterPolicy.createHeader(STHdrFtr.DEFAULT, paragraphs); – lshared Jul 23 '15 at 15:31
  • From the spec `Note: The pgNum Block is a legacy construct used for compatibility with older word processors, and should not be produced unless it was consumed while reading a document` – jmarkmurphy Feb 24 '17 at 12:24
0

Hope this helps.

XWPFParagraph p1 = doc.createParagraph();
XWPFRun r1 = p1.createRun();
r1.setText("Page ");
r1.getCTR().addNewPgNum();
r1.setText(" of 9");
p1.setAlignment(ParagraphAlignment.RIGHT);