1、引用Apache POI
使用Apache POI操作Word文件,需要在项目中包含了Apache POI的依赖。如使用Maven,可以在pom.xml文件中添加以下依赖,如下,
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
2、使用Apache POI生成页码
使用Apache POI在Word文档的页脚中添加页码,代码如下,
try (XWPFDocument document = new XWPFDocument()) {
// 创建一个段落
XWPFParagraph paragraphMain = document.createParagraph();
XWPFRun runMain = paragraphMain.createRun();
runMain.setText("Sample paragraph text.");
// 创建页脚并添加页码
//设置XWPFFooter类型脚标对象
XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);//创建一个新的XWPFFooter对象
XWPFParagraph paragraph = footer.createParagraph();//创建新的XWPFParagraph对象
paragraph.setAlignment(ParagraphAlignment.CENTER);//设置样式居中
//设置段落对象
XWPFRun runPre = paragraph.createRun();//新的段落对象
runPre.setText("- ");
//setXWPFRunStyle(runPre,"宋体",14);
XWPFRun run = paragraph.createRun();//新的段落对象
CTFldChar fldChar = run.getCTR().addNewFldChar();//新的CTFldChar对象
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
CTText ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("PAGE \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
//setXWPFRunStyle(run,"宋体",14);
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
//设置段落对象
XWPFRun runSuf = paragraph.createRun();//新的段落对象
runSuf.setText(" -");
//setXWPFRunStyle(runSuf,"宋体",14);
// 将页脚添加到所有的页面
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document);
headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, new XWPFParagraph[]{paragraph});
// 保存文档
try (FileOutputStream out = new FileOutputStream("WordWithPageNumbers.docx")) {
document.write(out);
}
} catch (IOException e) {
e.printStackTrace();
}
3、设置页脚的字体样式
Word中生成的页码的页脚样式如需要调整,可以参考代码如下,
/**
* 设置页脚的字体样式
* @param xr 段落元素
* @param font 段落元素
* @param fontSize 的大小
*/
static void setXWPFRunStyle(XWPFRun xr, String font, int fontSize) {
sr.setFontSize(fontSize);
CTRPr rpr = xr.getCTR().isSetRPr() ? xr.getCTR().getRPr() : xr.getCTR().addNewRPr();
CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
fonts.setAscii(font);
fonts.setEastAsia(font);
fonts.setHAnsi(font);
}
4、奇偶页格式
如奇数和偶数页的样式不同,可以先设置所有面的页码样式,之后在设置偶数页的样式。
1)设置所有页码
设置所有页码靠左显示。
XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);//创建一个新的XWPFFooter对象,HeaderFooterType.DEFAULT表示所有页
XWPFParagraph paragraph = footer.createParagraph();//创建新的XWPFParagraph对象
paragraph.setAlignment(ParagraphAlignment.LEFT);//设置页码靠左
//设置段落对象
XWPFRun runPre = paragraph.createRun();//新的段落对象
runPre.setText("- ");
//setXWPFRunStyle(runPre,"宋体",14);
XWPFRun run = paragraph.createRun();//新的段落对象
CTFldChar fldChar = run.getCTR().addNewFldChar();//新的CTFldChar对象
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
CTText ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("PAGE \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
//setXWPFRunStyle(run,"宋体",14);
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
//设置段落对象
XWPFRun runSuf = paragraph.createRun();//新的段落对象
runSuf.setText(" -");
2)设置偶数页码
设置偶数的页码靠右显示。
XWPFFooter footer = document.createFooter(HeaderFooterType.EVEN);//创建一个新的XWPFFooter对象,HeaderFooterType.EVEN 表示偶数页
XWPFParagraph paragraph = footer.createParagraph();//创建新的XWPFParagraph对象
paragraph.setAlignment(ParagraphAlignment.RIGHT);//设置页码靠右
//设置段落对象
XWPFRun runPre = paragraph.createRun();//新的段落对象
runPre.setText("- ");
//setXWPFRunStyle(runPre,"宋体",14);
XWPFRun run = paragraph.createRun();//新的段落对象
CTFldChar fldChar = run.getCTR().addNewFldChar();//新的CTFldChar对象
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
CTText ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("PAGE \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
//setXWPFRunStyle(run,"宋体",14);
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
//设置段落对象
XWPFRun runSuf = paragraph.createRun();//新的段落对象
runSuf.setText(" -");
5、页码和总页数
如显示页码时,需要添加总页数的显示,参考如下代码,
try (XWPFDocument document = new XWPFDocument()) {
// 创建一个段落
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Sample paragraph text.");
// 创建页脚
XWPFParagraph footerParagraph = new XWPFParagraph(CTP.Factory.newInstance(), document);
footerParagraph.setAlignment(ParagraphAlignment.CENTER);
CTP ctP = footerParagraph.getCTP();
CTPPr ctppr = ctP.addNewPPr();
CTString pst = ctppr.addNewPStyle();
pst.setVal("PageNumber");
// 添加页码到页脚
XWPFRun footerRun = footerParagraph.createRun();
footerRun.setText("第 ");
footerRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.Enum.forString("begin"));
footerRun.getCTR().addNewInstrText().setStringValue("PAGE \\* MERGEFORMAT");
footerRun.getCTR().addNewInstrText().setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
footerRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.Enum.forString("end"));
footerRun.setText(" 页 共");
footerRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.Enum.forString("begin"));
footerRun.getCTR().addNewInstrText().setStringValue("NUMPAGES \\* MERGEFORMAT");
footerRun.getCTR().addNewInstrText().setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
footerRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.Enum.forString("end"));
footerRun.setText("页 ");
// 将页脚添加到所有的页面
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document);
headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, new XWPFParagraph[]{footerParagraph});
// 保存文档
try (FileOutputStream out = new FileOutputStream("WordWithPageNumbers.docx")) {
document.write(out);
}
} catch (IOException e) {
e.printStackTrace();
}
参考文档:https://poi.apache.org/apidocs/dev/org/apache/poi/xwpf/usermodel/XWPFDocument.html