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

查看:129
本文介绍了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天全站免登陆