XWPF POI 页脚与最后一页不同
How to set footer diferrent only in last page. I mean, in last page no footer or different. in first page and other are same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Microsoft Word 中没有仅最后一页的页眉/页脚。
第一页、偶数页和默认页可以有不同的页眉/页脚设置。如果没有定义其他内容,则每个页面都会使用默认的页眉/页脚。因此,如果偶数页有页眉/页脚,并且设置奇数页和偶数页不同,则默认页眉/页脚仅用作奇数页的页眉/页脚。如果没有这样的设置,则每个页面都会使用默认的页眉/页脚。如果第一页设置有特殊的页眉/页脚,则第一页除外。
但是 Word 文档可以分为不同的部分。每个部分可能有自己的页眉/页脚设置。因此,当且仅当最后一页位于其自己的部分中时,才可以仅针对最后一页(最后部分)有不同的页眉/页脚。
不幸的是,
apache poi
不提供在XWPF
中设置节的方法。它只提供了设置文档中三种不同页眉/页脚类型的方法。因此,要将XWPFDocument
分成几个部分并为这些部分设置不同的页眉/页脚设置,需要一点点作弊。人们可以创建三种可能的页眉/页脚类型之一,只是为了稍后将其设置为自己的部分的默认页眉/页脚。以下示例说明了这一点。它为文档中的偶数页创建一个页脚,该页脚从未被使用,但后来被引用为最后一部分的默认页脚。请注意代码中的进一步注释。
当然,只有当人们至少知道最后一页从哪里开始时,这才有效。必须知道这一点,因为分节符需要设置在激光页面内容之前。
这已经过测试并且可以使用当前的
apache poi 5.2.0
运行。There is not a header/footer only for the last page in Microsoft Word.
There are different header/footer settings possible for first page, even pages and default pages. There the default header/footer gets used for each pages if not else is defined. So if there is a header/footer for even pages and a setting that odd and even pages shall be different, then default header/footer gets used as header/footer for odd pages only. If there is no such setting, then the default header/footer gets used for each page. This is except first page, if there is a special header/footer for first page set.
But a Word document can be separated into different sections. And each section may have own header/footer settings. So if, and only if, the last page is in its own section, then there can be a different header/footer for the last page (last section) only.
Unfortunately
apache poi
do not provide methods to set sections inXWPF
. And it only provides methods to set the three different header/footer types in document. So to get aXWPFDocument
separated into sections and to set different header/footer settings for those sections, a little bit cheating is necessary.One could create one of the three possible header/footer types only to set it to be the default header/footer for an own section later. The following example shows this. It creates a footer for even pages in document, which never gets used but later gets referenced as the default footer for the last section. Note the further comments in the code.
Of course that only can work if one at least knows where the last page starts. That must be known because the section break needs to be set before the content of the lase page.
This is tested and works using current
apache poi 5.2.0
.