刚开始使用POI 3.10创建Word文档(XWPF)。 大多数事情都是直截了当的,但我不明白如何添加页码。 我添加了页脚,但页脚中的文本在每页都是相同的
答案 0 :(得分:8)
我在LibreOffice右侧的页脚中创建了一个页码,并调查了XML文件(POI中不支持MS Word-Std-Objects,用于页码)。
这样可以创建更复杂的页脚......
将数字设置为其他位置为ctjc设置另一个值而不是STJc.RIGHT ......
结果如下:
// 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);
答案 1 :(得分: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();
}
答案 2 :(得分:0)
希望这会有所帮助。
XWPFParagraph p1 = doc.createParagraph();
XWPFRun r1 = p1.createRun();
r1.setText("Page ");
r1.getCTR().addNewPgNum();
r1.setText(" of 9");
p1.setAlignment(ParagraphAlignment.RIGHT);
答案 3 :(得分:0)
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 });
您在页脚中获得了Page 1 of 9
。