Hi, When I build a sample .docx and look at the the XML inside the docx, contains the following for the page-item in the header:
<w:p ...> ... <w:r> <w:instrText xml:space="preserve"> PAGE \* MERGEFORMAT </w:instrText> </w:r> w:p is CTP/XWPFParagraph, w:r is CTR/XWPFRun, and the CTR has a method addNewInstrText() which might do what you need here via the low-level APIs of POI. Domink. On Tue, May 24, 2016 at 6:04 PM, cathrop <cathr...@gmail.com> wrote: > I need a footer with 3 parts: the current date on the left, a field value > (in > this case the opportunity #) in the center and the page number on the right > preceded by the word "Page ". > > I am able to create this footer with the following code without an > incremented page number: > > CTSectPr sectPr = > doc.getDocument().getBody().addNewSectPr(); > XWPFHeaderFooterPolicy policy = new > XWPFHeaderFooterPolicy(doc, sectPr); > CTP footerCtp = CTP.Factory.newInstance(); > CTR footerCtr = footerCtp.addNewR(); > > XWPFParagraph footerCopyrightParagraph = new > XWPFParagraph(footerCtp, > doc); > > > doc.getProperties().getExtendedProperties().getUnderlyingProperties().getPages(); > XWPFRun run = footerCopyrightParagraph.getRun(footerCtr); > run.setText(dateOnlyFormat.format(calendar.getTime())); > run.addTab(); > run.setText("OppNo = Q2W3E4R"); > run.addTab(); > run.setText("Page "); > run.setFontSize(10); > run.setFontFamily("Arial"); > > BigInteger pos1 = BigInteger.valueOf(4500); > setTabStop(footerCtp, STTabJc.Enum.forString("center"), > pos1); > BigInteger pos2 = BigInteger.valueOf(9000); > setTabStop(footerCtp, STTabJc.Enum.forString("right"), > pos2); > > XWPFParagraph[] footerParagraphs = { > footerCopyrightParagraph }; > policy.createFooter(STHdrFtr.DEFAULT, footerParagraphs); > > this is the result: > May 24, 2016 OppNo = 1Q2W3E4R > Page > > I did find that by adding this line: > footerCtp.addNewR().addNewPgNum(); > > I get this result: > 1May 24, 2016 OppNo = 1Q2W3E4R > Page > and on the second page: > 2May 24, 2016 OppNo = 1Q2W3E4R > Page > > How can I tie the two together so the incremented page number is to the > right of the word "Page" like this? > > May 24, 2016 OppNo = 1Q2W3E4R > Page 1 > > > > -- > View this message in context: > http://apache-poi.1045710.n5.nabble.com/Add-complex-footer-to-docx-incrementing-the-page-number-tp5723124.html > Sent from the POI - User mailing list archive at Nabble.com. > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscr...@poi.apache.org > For additional commands, e-mail: user-h...@poi.apache.org > >