如何使用itext在pdf中添加多个页眉和页脚 [英] How to add multiple headers and footers in pdf using itext

查看:592
本文介绍了如何使用itext在pdf中添加多个页眉和页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的pdf中我需要有多个页眉和页脚。在标题中我想要左边的标题标题和中心的一些文字。



同样在页脚中我需要在左侧打印我的公司名称,在中心打印页码,并在右侧的表格中打印一些有关内容的信息。



我见过这么多帖子,但我没有得到正确的想法创建这个。有人请帮我一些示例代码片段。谢谢

解决方案

应使用页面事件添加页眉和页脚。如果您需要一些示例,只需查找关键字标题 / 页脚



只需创建一个类extends PdfPageEventHelper 并实现 onEndPage()方法。阅读文档的人并没有犯下使用 onStartPage()方法的常见错误,但也许您忽略了这一点,所以我将此添加为额外的警告。 / p>

使用 setPageEvent()方法将类的实例添加到PdfWriter对象。



我不知道我是否理解多个标题的含义。如果您有多个页面事件实现,则可以使用 setPageEvent()方法将它们全部添加,并且它们都将被执行。如果要从一个页面事件实现切换到另一个页面事件实现,则需要先使用 setPageEvent(null)



也许您希望不同页面的标题不同,只需在页面事件实现中使用成员变量并在此过程中进行更改。在其中一个名为 MovieHistory2的书籍示例中,标题的文本存储在 String 数组中,名为 header



标题的位置取决于页码:

  public void onEndPage(PdfWriter) writer,Document document){
Rectangle rect = writer.getBoxSize(art);
switch(writer.getPageNumber()%2){
case 0:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT,header [0],
rect.getRight(),rect.getTop(),0);
休息;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT,header [1],
rect.getLeft(),rect.getTop(), 0);
休息;
}
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER,new Phrase(String.format(page%d,pagenumber)),
(rect .getLeft()+ rect.getRight())/ 2,rect.getBottom() - 18,0);
}

对于偶数页码,标题添加到右侧;对于左侧的奇数页码。如您所见,页脚居中。



您还提到了标题表。如果您想使用表格,请查看 MovieCountries1 示例。



你说:我看过很多帖子,但我没有得到正确的想法来创建这个。您将通过阅读文档来获得正确的想法,更具体地说,是iText in Action - Second Edition一书中的第5章,我将从中引用代码片段。


In my pdf I need to have more than one header and footer.In header I want title heading on left and some text on the center.

Likewise in footer I need to print my company name on the left side,page number on center and some info regarding the contents in my table on the right side.

I have seen so many posts but I didn't get the correct idea for creating this.Somebody please help me with some example code snippets.Thanks

解决方案

Headers and footers should be added using 'page events'. If you need some examples, just look for the keyword header / footer on the official web site.

Just create a class that extends PdfPageEventHelper and implement the onEndPage() method. People who read the documentation do not make the common mistake to use the onStartPage() method, but maybe you overlooked this, so I'm adding this as an extra caveat.

Add an instance of your class to the PdfWriter object with the setPageEvent() method.

I don't know if I understand what you mean by "multiple" headers. If you have more than one page event implementation, you can add them all using the setPageEvent() method and they will all be executed. If you want to switch from one page event implementation to another, you need to use setPageEvent(null) first.

Maybe you want the header to be different for different pages, just use a member-variable in your page event implementation and change it along the way. In one of the book examples named MovieHistory2, the text for the header is stored in a String array named header.

The position of the header depends on the page number:

    public void onEndPage(PdfWriter writer, Document document) {
        Rectangle rect = writer.getBoxSize("art");
        switch(writer.getPageNumber() % 2) {
        case 0:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_RIGHT, header[0],
                    rect.getRight(), rect.getTop(), 0);
            break;
        case 1:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_LEFT, header[1],
                    rect.getLeft(), rect.getTop(), 0);
            break;
        }
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),
                (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
    }

For even page numbers, the header is added to the right; for odd page numbers to the left. The footer is centered as you can see.

You also mention a header table. If you want to use a table, please take a look at the MovieCountries1 example.

You say: "I have seen so many posts but I didn't get the correct idea for creating this." You will get the correct idea by reading the documentation, more specifically chapter 5 of the book "iText in Action — Second Edition" from which the code snippets I'm referring to are taken.

这篇关于如何使用itext在pdf中添加多个页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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