POI API 中是否指定了任何方法来获取总页数,我可以在文档的页脚中添加页码,但无法添加总页数值。
页数Word
取决于很多因素,例如字体大小、段落顶部/底部边距和填充、打印机设置、手动插入的分页符等。所以不能直接存到文件中。它将在运行时计算Word
正在渲染其页面。
但是我们可以使用页脚中的字段来计算页码和总页数。
使用最多的示例apache poi 3.14
:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
//import org.apache.poi.wp.usermodel.HeaderFooterType;
public class CreateWordHeaderFooter {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body:");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.setText("Lorem ipsum.... page 1");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("Lorem ipsum.... page 2");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("Lorem ipsum.... page 3");
// create header-footer
XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();
// create header start
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
//XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("The Header:");
// create footer start
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
//XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.getParagraphArray(0);
if (paragraph == null) paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("Page ");
paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = paragraph.createRun();
run.setText(" of ");
paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
FileOutputStream out = new FileOutputStream("CreateWordHeaderFooter.docx");
doc.write(out);
out.close();
doc.close();
}
}
中的字段Word
那么是{PAGE \* MERGEFORMAT}
and {NUMPAGES \* MERGEFORMAT}
.
对于使用电流apache poi 4.1.2
无需XWPFHeaderFooterPolicy
using XWPFDocument.createHeader
, XWPFDocument.createFooter
and HeaderFooterType
:
...
//import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.wp.usermodel.HeaderFooterType;
...
// create header-footer
//XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
//if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();
// create header start
//XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
...
// create footer start
//XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)