简体   繁体   English

apache poi 禁用首页的默认页脚

[英]apache poi disable default footer for the first page

I am trying to create a word document in which I will have no footer only in the first page and a footer for the rest of the pages.我正在尝试创建一个 word 文档,其中只有首页没有页脚,页面的 rest 的页脚。 I wrote the following code (I also tried to change -reverse- the order of creation of footer and footerFirst objects) but that did not help.我编写了以下代码(我还尝试更改 -reverse- footerfooterFirst对象的创建顺序),但这没有帮助。 I still have the default footer on all pages.我仍然在所有页面上都有默认页脚。

How should I disable the footer from the first page?我应该如何禁用第一页的页脚? Thanks in advance.提前致谢。

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

That there is a different header set for first page only, means not that this header will also be shown.仅第一页设置了不同的 header,这并不意味着该 header 也会显示。 In Word GUI there is a checkbox [x] Different First Page in Header & Footer Tools to achieve that.Word GUI中,在 Header 和页脚工具中有一个复选框[x] Different First Page来实现这一点。

And according Office Open XML Part 4 - Markup Language Reference there must a boolean XML element titlePg be set to determine that there is a title page present.根据Office Open XML Part 4 - Markup Language Reference ,必须设置 boolean XML元素titlePg以确定存在标题页。

In old apache poi versions using XWPFHeaderFooterPolicy this XML element titlePg can only be set using underlying low level objects using document.getDocument().getBody().getSectPr().addNewTitlePg();在使用XWPFHeaderFooterPolicy的旧apache poi版本中,此XML元素titlePg只能使用底层低级对象使用document.getDocument().getBody().getSectPr().addNewTitlePg(); . .

But using current apache poi versions (since 3.16 ) there is no need using XWPFHeaderFooterPolicy directly.但是使用当前的apache poi版本(自3.16起)无需直接使用XWPFHeaderFooterPolicy Now there is XWPFDocument.createHeader and XWPFDocument.createFooter using a HeaderFooterType .现在有XWPFDocument.createHeaderXWPFDocument.createFooter使用HeaderFooterType This sets titlePg flag in XML when HeaderFooterType.FIRST is used.当使用HeaderFooterType.FIRST时,这会在XML中设置titlePg标志。

Complete example which sets and uses HeaderFooterType.FIRST and HeaderFooterType.DEFAULT :设置和使用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();


 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM