如何在poi word XWPF的首页和其他页面中生成不同的页眉? [英] How to generate different headers in first page and other pages in poi word XWPF?
问题描述
我想在poi word的首页和其他页面中生成不同的标题,因此我使用了XWPFHeaderFooterPolicy.FIRST和XWPFHeaderFooterPolicy.DEFAULT.当我使用XWPFHeaderFooterPolicy.DEFAULT时,我可以成功插入标头,但是当我更改为XWPFHeaderFooterPolicy.FIRST时,我在第一页中看不到标头,这是下面的代码,这是怎么了?谢谢!
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");
推荐答案
仅为首页设置了不同的标题,这意味着也不会显示此标题.在Word
GUI中,在Header & Footer Tools
中有一个复选框[x] Different First Page
用于实现该目标.
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.
并且根据 Office Open XML Part 4-标记语言参考,必须设置一个布尔XML元素titlePg
才能确定是否存在标题页.
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.
在实际的apache poi
最终版本3.15中,只能使用doc.getDocument().getBody().getSectPr().addNewTitlePg();
使用底层低级对象来设置此XML元素titlePg
.
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();
.
但是apache poi
版本3.16 Beta 2具有doc.createHeader(HeaderFooterType.FIRST);
,它在XML中设置了titlePg
标志.
But apache poi
version 3.16 Beta 2 has doc.createHeader(HeaderFooterType.FIRST);
which sets titlePg
flag in XML.
完整示例:
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"));
}
}
但是我认为在创建HeaderFooterType.FIRST
时自动在XML中设置titlePg
标志是不正确的.由于Word
可以在[x] Different First Page
和[] Different First Page
之间切换,因此apache poi
也应该可以.因此,设置titlePg
标志应该是XWPFDocument
中的一种方法.
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的首页和其他页面中生成不同的页眉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!