问题描述
我想在poi word的第一页和其他页面中生成不同的标题,所以我使用了XWPFHeaderFooterPolicy.FIRST和XWPFHeaderFooterPolicy.DEFAULT.当我使用 XWPFHeaderFooterPolicy.DEFAULT 时,我可以成功插入我的标题,但是当我更改为 XWPFHeaderFooterPolicy.FIRST 时,我看不到我的第一页中有标题,这是我的代码在下面,有什么问题?谢谢!
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.FIRST);段落 = header.createParagraph();段落.setAlignment(ParagraphAlignment.LEFT);运行 = 段落.createRun();run.setText("header");
只有第一页有一个不同的标题设置,并不意味着这个标题也会被显示.在 Word
GUI 中有一个复选框 [x] Different First Page
在 Header &页脚工具
来实现这一点.
并根据 Office Open XML 第 4 部分 - 标记语言参考必须设置一个布尔 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.*;导入 org.apache.poi.wp.usermodel.*;导入 org.apache.poi.xwpf.usermodel.*;公共类 CreateWordHeaderFooterDifferent {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 页");//创建第一页标题XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);段落 = header.createParagraph();段落.setAlignment(ParagraphAlignment.LEFT);运行 = 段落.createRun();run.setText("第一页标题:");//创建默认页眉header = doc.createHeader(HeaderFooterType.DEFAULT);段落 = header.createParagraph();段落.setAlignment(ParagraphAlignment.LEFT);运行 = 段落.createRun();run.setText("默认页眉:");//创建页脚XWPFFooter 页脚 = doc.createFooter(HeaderFooterType.DEFAULT);段落=footer.createParagraph();段落.setAlignment(ParagraphAlignment.CENTER);运行 = 段落.createRun();run.setText("页面");段落.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");运行 = 段落.createRun();run.setText(" of ");段落.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");doc.write(new FileOutputStream("CreateWordHeaderFooterDifferent.docx"));}}
但在我看来,在创建 HeaderFooterType.FIRST
时自动在 XML 中设置 titlePg
标志是不正确的.由于 Word
可以在 [x] Different First Page
和 [] Different First Page
之间切换,apache poi
也应该能够这样做.所以设置titlePg
标志应该是XWPFDocument
中的一个方法.
I want to generate different headers in first page and other pages in poi word, So I used XWPFHeaderFooterPolicy.FIRST and XWPFHeaderFooterPolicy.DEFAULT. when I using XWPFHeaderFooterPolicy.DEFAULT I can insert my header successfully, but when I change to XWPFHeaderFooterPolicy.FIRST, I cannot see there is a header in my first page, this is my code in below, what's wrong with it? thanks!
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("header");
That there is a different header set for first page only, means not that this header will also be shown. In Word
GUI there is a checkbox [x] Different First Page
in Header & Footer Tools
to achieve that.
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.
In actual final apache poi
version 3.15 this XML element titlePg
can only be set using underlying low level objects using doc.getDocument().getBody().getSectPr().addNewTitlePg();
.
But apache poi
version 3.16 Beta 2 has doc.createHeader(HeaderFooterType.FIRST);
which sets titlePg
flag in XML.
Complete example:
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"));
}
}
But in my opinion the setting titlePg
flag in XML automatically while HeaderFooterType.FIRST
is created is not correct. Since Word
can toggle between [x] Different First Page
and [] Different First Page
, apache poi
should also be able to do so. So the setting the titlePg
flag should be a method in XWPFDocument
.
这篇关于如何在poi word XWPF的第一页和其他页面中生成不同的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!