Show page numbers in PDF using ITEXT

2019-05-13 17:08发布

问题:

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.

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);

回答1:

The showTextAligned function takes the following parameters.

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

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);

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.



标签: java itext