Inserting a page number is a 3 step operation:
1. create and add the header or footer part in which it is to appear
2. insert the page number in the header/footer contents in the header/footer part
3. add reference to that header/footer to sectPr
Step 1: - Code: Select all
HeaderPart headerPart = new HeaderPart();
Relationship rel = wordprocessingMLPackage.getMainDocumentPart()
.addTargetPart(headerPart);
Step 2: A footer, as created by Word 2007, might look like:
- Code: Select all
<w:ftr >
<w:sdt>
<w:sdtPr>
<w:id w:val="349237360"/>
<w:docPartObj>
<w:docPartGallery w:val="Page Numbers (Bottom of Page)"/>
<w:docPartUnique/>
</w:docPartObj>
</w:sdtPr>
<w:sdtContent>
<w:p>
<w:pPr>
<w:pStyle w:val="Footer"/>
<w:jc w:val="center"/>
</w:pPr>
<w:fldSimple w:instr=" PAGE \* MERGEFORMAT ">
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:t>17</w:t>
</w:r>
</w:fldSimple>
</w:p>
</w:sdtContent>
</w:sdt>
<w:p>
<w:pPr>
<w:pStyle w:val="Footer"/>
</w:pPr>
</w:p>
</w:ftr>
but you only really need:
- Code: Select all
<w:ftr >
<w:p>
<w:pPr>
<w:pStyle w:val="Footer"/>
<w:jc w:val="center"/>
</w:pPr>
<w:fldSimple w:instr=" PAGE \* MERGEFORMAT ">
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:t>17</w:t>
</w:r>
</w:fldSimple>
</w:p>
</w:ftr>
w:fldSimple being the critical bit. There are in general 2 approaches to creating these objects - see the Getting Started guide for details.
- Code: Select all
headerPart.setJaxbElement(yourFooter);
Step 3Using the relId from the rel in step 1, add your
w:footerReference to w:sectPr:
- Code: Select all
<w:sectPr>
<w:footerReference w:type="default" r:id="rId40"/>
(HeaderFooterPolicy would be a good place to add code to help with steps 1 and 3, but at present it only helps with reading existing headers/footers.)
Removing page numbers would be a full or partial reversal of the above.
cheers .. Jason