apache poi 禁用第一页的默认页脚

apache poi disable default footer for the first page

我正在尝试创建一个 word 文档,其中只有第一页没有页脚,其余页面都有页脚。我编写了以下代码(我还尝试更改 -reverse- footerfooterFirst 对象的创建顺序)但这没有帮助。我仍然在所有页面上使用默认页脚。

我应该如何禁用第一页的页脚?提前致谢。

private XWPFDocument initDocument(String FILE) throws Exception{
    XWPFDocument document = new XWPFDocument();

    XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
    if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy();


    // create header start
    XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

    //XWPFParagraph paragraph = footer.createParagraph();
    XWPFParagraph paragraph = footer.getParagraphArray(0);
    if (paragraph == null)
        paragraph = footer.createParagraph();
    paragraph.setAlignment(ParagraphAlignment.CENTER);

    XWPFRun run = paragraph.createRun();
    run.setFontSize(11);
    run.setFontFamily("Times New Roman");
    run.setText("Some company info in the footer");

    XWPFFooter footerFirst = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.FIRST);
    paragraph = footerFirst.getParagraphArray(0);
    if (paragraph == null)
        paragraph = footerFirst.createParagraph();
    paragraph.setAlignment(ParagraphAlignment.CENTER);

    run = paragraph.createRun();
    run.setText(" ");



    return document;
}

仅第一页有不同的 header 设置,并不表示此 header 也会显示。在 Word GUI 中,Header 和页脚工具中有一个复选框 [x] Different First Page 来实现这一点。

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

在使用 XWPFHeaderFooterPolicy 的旧 apache poi 版本中,此 XML 元素 titlePg 只能使用底层低级别 objects 使用 document.getDocument().getBody().getSectPr().addNewTitlePg(); 进行设置.

但是使用当前的 apache poi 版本(自 3.16 起)不需要直接使用 XWPFHeaderFooterPolicy 。现在有 XWPFDocument.createHeaderXWPFDocument.createFooter 使用 HeaderFooterType。当使用 HeaderFooterType.FIRST 时,这会在 XML 中设置 titlePg 标志。

设置和使用HeaderFooterType.FIRSTHeaderFooterType.DEFAULT的完整示例:

import java.io.FileOutputStream;

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

public class CreateWordFooters {

 public static void main(String[] args) throws Exception {
     
  XWPFDocument document = new XWPFDocument();

  // the body content
  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("The Body... first page");
  
  paragraph = document.createParagraph();
  run=paragraph.createRun();
  run.addBreak(BreakType.PAGE); 
  run.setText("The Body... second page");

  // create first page footer
  XWPFFooter footer = document.createFooter(HeaderFooterType.FIRST);
  paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  run = paragraph.createRun();
  run.setText("First page footer...");

  // create default footer
  footer = document.createFooter(HeaderFooterType.DEFAULT);
  paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);
  run = paragraph.createRun();
  run.setText("Default footer...");

  FileOutputStream out = new FileOutputStream("CreateWordFooters.docx");
  document.write(out);
  out.close();
  document.close();


 }
}