Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI how to add a page number

Tags:

apache-poi

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

like image 253
michael shapira Avatar asked Dec 10 '14 21:12

michael shapira


2 Answers

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);
like image 179
Sempfer Avatar answered Sep 20 '22 18:09

Sempfer


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.

like image 30
Grigory K Avatar answered Sep 20 '22 18:09

Grigory K