我想在apache poi库生成的word文档中添加页脚,我的方法总是只在最后一页添加页脚文本的问题,我错过了什么?谢谢, 这吼我的方法
private void addWordFooter(XWPFDocument document, CTBody body, String clientDate,
String graphName, long TabWidth) throws IOException, InvalidFormatException {
CTSectPr sectPr = body.getSectPr();
if(sectPr==null)
{
sectPr = body.addNewSectPr();
}
CTP footerCtp = CTP.Factory.newInstance();
CTR footerCtr = footerCtp.addNewR();
XWPFParagraph footerCopyrightParagraph = new XWPFParagraph(footerCtp, document);
document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
XWPFRun run = footerCopyrightParagraph.getRun(footerCtr);
run.setText(graphName);
run.addTab();
run.setText(clientDate);
setTabStop(footerCtp, STTabJc.Enum.forString("right"), BigInteger.valueOf(TabWidth));
XWPFParagraph[] footerParagraphs = { footerCopyrightParagraph };
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, footerParagraphs);
}
setTabStop方法:
private void setTabStop(CTP oCTP, STTabJc.Enum oSTTabJc, BigInteger oPos) {
CTPPr oPPr = oCTP.getPPr();
if (oPPr == null) {
oPPr = oCTP.addNewPPr();
}
CTTabs oTabs = oPPr.getTabs();
if (oTabs == null) {
oTabs = oPPr.addNewTabs();
}
CTTabStop oTabStop = oTabs.addNewTab();
oTabStop.setVal(oSTTabJc);
oTabStop.setPos(oPos);
}
答案 0 :(得分:0)
经过一些测试后,我认为你只在最后一个身体上调用这个功能。
从CTBody body
删除此参数addWordFooter()
。
并将此行添加到您的功能
CTSectPr sectPr = d.getDocument().getBody().addNewSectPr();
它会将页脚应用于整个.docx。
与您的问题相反,我尝试仅将页脚添加到最后一页,并通过传递特定CTBody body
我可以复制您的问题。
答案 1 :(得分:0)
尝试一下:
private void addWordFooter(XWPFDocument document, String text) throws IOException {
CTP ctp = CTP.Factory.newInstance();
CTText t = ctp.addNewR().addNewT();
t.setStringValue(text);
XWPFParagraph pars[] = new XWPFParagraph[1];
pars[0] = new XWPFParagraph(ctp, document);
XWPFHeaderFooterPolicy hfp = document.createHeaderFooterPolicy();
hfp.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars);
//hfp.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
}