700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java生成word文件带上页码 使用apache poi api创建Word文档时 如何以Y的X格式添加页码?...

java生成word文件带上页码 使用apache poi api创建Word文档时 如何以Y的X格式添加页码?...

时间:2020-09-23 03:09:51

相关推荐

java生成word文件带上页码 使用apache poi api创建Word文档时 如何以Y的X格式添加页码?...

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);

...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。