需求: 添加页码
<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 });
}
}