POI 为 word docx 文件中的第一页启用不同的页眉/页脚 [英] POI enable different header/footer for the first page in word docx file

查看:80
本文介绍了POI 为 word docx 文件中的第一页启用不同的页眉/页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache POI 3.13 生成一个 docx 文件,并且我坚持使用第一页的页眉/页脚.我创建 XMPFParagraph[] 没有任何问题.接下来我像这样创建页眉和页脚(我尝试过不同的 oreder):

I'm generating a docx file using Apache POI 3.13 and I stuck with headers/footers for first page. I create XMPFParagraph[] without any problem. Next I create headers and footers like this (I've tried in different oreder):

policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, defaultHeader);
policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, defaultFooter);
policy.createHeader(XWPFHeaderFooterPolicy.FIRST, firstHeader);
policy.createFooter(XWPFHeaderFooterPolicy.FIRST, firstFooter);

一旦我生成了我的 docx 文件,我就可以在包括第一页在内的每一页上看到我的默认页眉/页脚.但是,如果我选择对第一页使用不同的页眉/页脚 - 我的第一个页眉和页脚会正确显示.我怎么能通过代码自动发生这种情况?是否有任何适当的文档包含有关 POI 的示例?

Once I generate my docx file I could see my default header/footer on every page including first one. But if I select to use different header/footer for the first page - my first header and footer apperes correctly. How could I make this happens automaticaly via code? And is there any appropriate documentation with examples about POI?

推荐答案

如果您想在一个部分中设置首页标题,您必须在部分属性标签 (w:sectPr) 中输入一个标题页标签.标题页标签可以为空,但这是必要的.在您的情况下,您只能添加 2 行代码:

If you want to set a first page header in a section, you must enter a title page tag in section properties tag (w:sectPr). The title page tag can be empty, but it is necessary. In your case, you can add only 2 code lines:

CTSectPr sect = document.getDocument().getBody().getSectPr();
sect.addNewTitlePg();

`最好的问候!

这篇关于POI 为 word docx 文件中的第一页启用不同的页眉/页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆