Apache的POI如何添加页码 [英] Apache POI how to add a page number

查看:975
本文介绍了Apache的POI如何添加页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用POI 3.10创建一个Word文档(XWPF)。
大部分的东西是直线前进,但我不知道如何添加页码。
我加了脚注,但在页脚文字是每一页

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

推荐答案

我创建了在LibreOffice的右侧页脚页码,并研究了XML文件(微软Word-STD-对象不支持POI其中使用那里页码)。

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...

设置来设置ctjc而不是STJc.RIGHT ...

to set the number to other positions set another value for ctjc instead of 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 everything from the footerXXX.xml you need
CTR 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);

这篇关于Apache的POI如何添加页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆