需求: 添加页码
<properties> <easypoi.version>4.0.0</easypoi.version> </properties> <!--
easypoi--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base
</artifactId> <version>${easypoi.version}</version> </dependency> <dependency> <
groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>${
easypoi.version}</version> </dependency> <dependency> <groupId>cn.afterturn</
groupId> <artifactId>easypoi-annotation</artifactId> <version>${easypoi.version}
</version> </dependency> package com.platform.modules.sas.utils; import org.
apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.
usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.io.*; /** * ClassName FooterTest * Description add footer to word
with poi * Author Kalinda * Date 2020/7/1 13:54 * Version 1.0 */ public class
FooterTest { public static void main(String[] args) { try { // file path File is
= new File("D:/test.docx"); FileInputStream fis = new FileInputStream(is); //
document object XWPFDocument doc = new XWPFDocument(fis); // calling method
createFooter(doc); // or this method both ok! // createFooter(doc); // output
OutputStream os= new FileOutputStream("D:\\Test1.docx"); doc.write(os); } catch
(Exception e) { e.printStackTrace(); } } public static void createFooter(
XWPFDocument doc) { // 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.CENTER); 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); } public static void
createDefaultFooter(final XWPFDocument document) { CTP pageNo = CTP.Factory.
newInstance(); XWPFParagraph footer = new XWPFParagraph(pageNo, document);
CTPPr begin= pageNo.addNewPPr(); begin.addNewPStyle().setVal("style21"); begin.
addNewJc().setVal(STJc.CENTER); pageNo.addNewR().addNewFldChar().setFldCharType(
STFldCharType.BEGIN); pageNo.addNewR().addNewInstrText().setStringValue("PAGE
\\* MERGEFORMAT"); pageNo.addNewR().addNewFldChar().setFldCharType(STFldCharType
.SEPARATE); CTR end = pageNo.addNewR(); CTRPr endRPr = end.addNewRPr(); endRPr.
addNewNoProof(); endRPr.addNewLang().setVal("zh-CN"); end.addNewFldChar().
setFldCharType(STFldCharType.END); CTSectPr sectPr = document.getDocument().
getBody().isSetSectPr() ? document.getDocument().getBody().getSectPr() : docx.
getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy policy = new
XWPFHeaderFooterPolicy(document, sectPr); policy.createFooter(STHdrFtr.DEFAULT,
new XWPFParagraph[] { footer }); } }

技术
©2019-2020 Toolsou All rights reserved,
【贪心算法】哈夫曼编码问题VHDL——JK触发器react 项目--博客系统数据库期末复习:综合应用题汇总面过了,起薪30k找出游戏的获胜者(java)JAVA实验四集合与函数式编程实验排序会了递归,不学非递归太可惜了SQL综合题 员工单位综合题数据库作业五