如何在第一页和poi字XWPF中的其他页面中生成不同的标题?

时间:2017-03-31 02:58:33

标签: apache-poi

我想在第一页和poi word中的其他页面生成不同的标题,所以我使用了XWPFHeaderFooterPolicy.FIRST和XWPFHeaderFooterPolicy.DEFAULT。当我使用XWPFHeaderFooterPolicy.DEFAULT时,我可以成功插入我的标题,但是当我更改为XWPFHeaderFooterPolicy.FIRST时,我看不到我的第一页中有一个标题,这是我下面的代码,它有什么问题?谢谢!

XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.FIRST);
      paragraph = header.createParagraph();
      paragraph.setAlignment(ParagraphAlignment.LEFT);
      run = paragraph.createRun();  
      run.setText("header");

2 个答案:

答案 0 :(得分:2)

该类中没有任何内容可以告诉Word显示不同的首页标题。您将需要最新版本的POI,然后使用以下命令从XWPFDocument创建标题:

XWPFDocument.createHeader(HeaderFooterType.FIRST);

否则你需要进入CT类,并在section属性中设置titlePg属性。我不推荐这种方法,因为它可能会改变。

答案 1 :(得分:1)

仅为第一页设置了不同的标头,意味着不会显示此标头。在Word GUI中,[x] Different First Page中有一个复选框Header & Footer Tools来实现此目的。

根据Office Open XML Part 4 - Markup Language Reference,必须设置一个布尔XML元素titlePg以确定是否存在标题页。

在实际的最终apache poi版本3.15中,此XML元素titlePg只能使用基于doc.getDocument().getBody().getSectPr().addNewTitlePg();的基础低级对象进行设置。

apache poi版本3.16 Beta 2有doc.createHeader(HeaderFooterType.FIRST);,用于在XML中设置titlePg标记。

完整示例:

import java.io.*;

import org.apache.poi.wp.usermodel.*;

import org.apache.poi.xwpf.usermodel.*;

public class CreateWordHeaderFooterDifferent {

 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 first page header
  XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);

  paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);

  run = paragraph.createRun();  
  run.setText("The first page header:");

  // create default page header
  header = doc.createHeader(HeaderFooterType.DEFAULT);

  paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);

  run = paragraph.createRun();  
  run.setText("The default page header:");

  // create footer
  XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);

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

  doc.write(new FileOutputStream("CreateWordHeaderFooterDifferent.docx"));

 }
}

但在我看来,在titlePg创建时自动设置HeaderFooterType.FIRST标志是不正确的。由于Word可以在[x] Different First Page[] Different First Page之间切换,apache poi也应该可以这样做。因此,设置titlePg标志应该是XWPFDocument中的方法。