使用ITEXT以PDF格式显示页码 [英] Show page numbers in PDF using ITEXT

查看:1415
本文介绍了使用ITEXT以PDF格式显示页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Itext在我们的Web应用程序中生成PDF。 PDF正在正确生成。

We are using Itext for generating the PDF's in our web application. The PDF's are getting generated properly.

我需要在页脚中显示一些版权信息以及页码。我需要在底部左侧显示版权信息,而在底部右侧显示页码。

I need to show some copyright information in the footer along with page numbers. I need to show the copyright information on the left hand side at the bottom while the page numbers on the right hand side at the bottom.

我已经浏览了谷歌和一些文章,使用它们我可以在底部添加页脚。但是这个页脚仅显示底部右侧的版权。

I have gone through google and some articles, using them I could add footer at the bottom. But this footer displays only copyright at the right hand side in the bottom.

如何使用Itext为此添加页码?

How can I add page numbers to this using Itext?

以下是我用来生成的代码页脚右侧的版权信息。

Below is the code I am using to generate the copyright information on the right hand side at the footer.

    static class HeaderFooter extends PdfPageEventHelper {
    public void onEndPage(PdfWriter writer, Document document) {
        Rectangle rect = writer.getBoxSize("footer");
        BaseFont bf_times;
        try {
            bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252",
                    false);
            Font font = new Font(bf_times, 9);
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_LEFT, new Phrase("Copyright 2011",
                            font), (rect.getLeft() + rect.getRight()) / 2,
                    rect.getBottom() - 18, 0);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以下是代码用于显示页码。我无法将这两者结合起来同时显示页码和版权信息。

As well below is the code which is used to show the page numbers. I am unable to combine both of these to show the page numbers and the copyright information at the same time.

ColumnText.showTextAligned(writer.getDirectContent(),
    Element.ALIGN_RIGHT,
    new Phrase(String.format("%d", writer.getPageNumber()),
            font), (rect.getLeft() + rect.getRight()) / 2,
    rect.getBottom() - 18, 0);


推荐答案

showTextAligned函数采用以下参数。

The showTextAligned function takes the following parameters.

public static void showTextAligned(PdfContentByte canvas,
                                   int alignment,
                                   Phrase phrase,
                                   float x,
                                   float y,
                                   float rotation)

在您的示例中,您正在设置两个项目的x值相同,因此您可能正在将一个文本写在另一个上。尝试调整你的x值,看看是否有帮助。

In your example, you are setting the x-values for both items to the same value, so you may be writing one piece of text over the other. Try adjusting your x-values to see if that helps.

具体来说,尝试设置页码编号块,使其与页脚矩形的右侧对齐。

Specifically, try setting the page number block of code so that is aligned with the right side of your footer Rectangle.

ColumnText.showTextAligned(writer.getDirectContent(),
    Element.ALIGN_RIGHT,
    new Phrase(String.format("%d", writer.getPageNumber()),
            font),  rect.getRight(),
    rect.getBottom() - 18, 0);

此外,您可以使用PDFStamper将事实后的页眉和页脚添加到PDF文档中。有时这可能是一种更简单的方法。 iText in Action书中有几个例子。 以下是其中一个的代码示例。

Additionally, you can use the PDFStamper to add headers and footers after the fact to a PDF document. Sometimes this can be a simpler approach. The iText in Action book has several examples of this. Here's the code sample for one of them.

这篇关于使用ITEXT以PDF格式显示页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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