使用apache poi api创建word文档时如何以X of Y格式添加页码? [英] How to add page numbers in format X of Y while creating a word document using apache poi api?

查看:51
本文介绍了使用apache poi api创建word文档时如何以X of Y格式添加页码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

POI API 中是否指定了任何方法来获取总页数,我可以在文档的页脚中添加页码,但我无法添加总页数值.

解决方案

Word 中的页数取决于很多因素,例如字体大小、段落顶部/底部边距和填充、打印机设置、手动插入分页符等.所以它不能直接存储在文件中.它将在 Word 呈现其页面时即时计算.

但是我们可以使用页脚中的字段来计算页码和总页数.

使用最多apache poi 3.14的示例:

import java.io.*;导入 org.apache.poi.xwpf.usermodel.*;导入 org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;//导入 org.apache.poi.wp.usermodel.HeaderFooterType;公共类 CreateWordHeaderFooter {public static void main(String[] args) 抛出异常 {XWPFDocument doc= new XWPFDocument();//正文内容XWPFParagraph 段落 = doc.createParagraph();XWPFRun run=paragraph.createRun();run.setText("The Body:");段落 = doc.createParagraph();运行=paragraph.createRun();run.setText("Lorem ipsum .... page 1");段落 = doc.createParagraph();运行=paragraph.createRun();运行.addBreak(BreakType.PAGE);run.setText("Lorem ipsum.... 第 2 页");段落 = doc.createParagraph();运行=paragraph.createRun();运行.addBreak(BreakType.PAGE);run.setText("Lorem ipsum.... 第 3 页");//创建页眉页脚XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();//创建头开始XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);//XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);段落 = header.getParagraphArray(0);if (paragraph == null)paragraph = header.createParagraph();段落.setAlignment(ParagraphAlignment.LEFT);运行 = 段落.createRun();run.setText("标题:");//创建页脚开始XWPFFooter 页脚 = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);//XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);段落 = 页脚.getParagraphArray(0);如果(段落==空)段落=footer.createParagraph();段落.setAlignment(ParagraphAlignment.CENTER);运行 = 段落.createRun();run.setText("页面");段落.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");运行 = 段落.createRun();run.setText(" of ");段落.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");FileOutputStream out = new FileOutputStream("CreateWordHeaderFooter.docx");doc.write(out);关闭();doc.close();}}

Word 中的字段是 {PAGE \* MERGEFORMAT}{NUMPAGES \* MERGEFORMAT}.

对于使用当前的 apache poi 4.1.2 可以在没有 XWPFHeaderFooterPolicy 的情况下使用 XWPFDocument.createHeaderXWPFDocument.createFooterHeaderFooterType:

<预><代码>...//导入 org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;导入 org.apache.poi.wp.usermodel.HeaderFooterType;...//创建页眉页脚//XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();//if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();//创建头开始//XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);...//创建页脚开始//XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);XWPFFooter 页脚 = doc.createFooter(HeaderFooterType.DEFAULT);...

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.

解决方案

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.

Example using up to 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();

 }
}

The fields in Word then are {PAGE \* MERGEFORMAT} and {NUMPAGES \* MERGEFORMAT}.

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文档时如何以X of Y格式添加页码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆