gpt4 book ai didi

java - 使用 apache poi api 创建 Word 文档时,如何添加 X of Y 格式的页码?

转载 作者:行者123 更新时间:2023-12-02 09:44:14 29 4
gpt4 key购买 nike

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}{NUMPAGES\* MERGEFORMAT}

对于使用当前的apache poi 4.1.2,无需XWPFHeaderFooterPolicy,使用XWPFDocument.createHeaderXWPFDocument.createFooter<即可完成此操作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);
...

关于java - 使用 apache poi api 创建 Word 文档时,如何添加 X of Y 格式的页码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385707/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com