使用apache poi api创建Word文档时,如何以Y的X格式添加页码? [英] How to add page numbers in format X of Y while creating a word document using apache poi api?
问题描述
在POI API中是否指定了任何方法来获取总页数,我可以在文档的页脚中添加页数,但无法添加总页数值.
Is there any method specified in POI API to get the total number of pages, I am able to add page number in the footer of the document but i am not able to add the total number of pages value.
推荐答案
Word
中的页数取决于很多因素,例如字体大小,段落顶部/底部页边距和填充,打印机设置,手动插入的分页符等. .因此,它不能直接存储在文件中.当Word
呈现其页面时,将即时计算它.
Page count in Word
is dependent of much things like font size, paragraph top/bottom margins and padding, printer settings, manually inserted page breaks and so on. So it cannot be stored directly in the file. It will be calculated on the fly while Word
is rendering its pages.
但是我们可以使用页脚中的字段来计算页码和总页数.
But we can use fields within the footer which also calculate the page number and the total number of pages.
示例最多使用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
,可以使用XWPFDocument.createHeader
,XWPFDocument.createFooter
和HeaderFooterType
而不使用XWPFHeaderFooterPolicy
来做到这一点:
For using current apache poi 4.1.2
one can do this without 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);
...
这篇关于使用apache poi api创建Word文档时,如何以Y的X格式添加页码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!