Hi all,

I'm having trouble using the PdfPageEventHelper class. The most significant part of the code:


    @Override
    public void onStartPage(PdfWriter writer, Document document) {
        try {
            PdfContentByte cb = writer.getDirectContentUnder();
            BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
            cb.beginText();
            cb.setColorFill (Color.LIGHT_GRAY);
            cb.setFontAndSize(bf, 140);
            cb.showTextAligned(Element.ALIGN_LEFT, text, 100, 100, 60);
            cb.endText();
            if (pageNumbers) {
                if(tpl == null){
                    tpl = writer.getDirectContent().createTemplate(500, 100);
                    tpl.setBoundingBox(new Rectangle(-20, -20, 500, 100));
                }
                pageTextFont = BaseFont.createFont ("Helvetica", BaseFont.WINANSI, false);
                String pageNumber = pageText + " " + (writer.getPageNumber() - 1) + " " + ofText;
                float pageTextSize = pageTextFont.getWidthPoint (pageNumber, 10);
                cb.beginText();
                cb.setFontAndSize(pageTextFont, 10);
                cb.setTextMatrix(document.left(), document.bottom() - 20);
                cb.showText(pageNumber);
                cb.endText();
                cb.addTemplate(tpl, document.left() + pageTextSize, document.bottom() - 20);
                cb.saveState();
                cb.restoreState();
            }

        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        } catch (IOException ioe) {
            throw new ExceptionConverter(ioe);
        }

    }

    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        if (pageNumbers) {
            tpl.beginText();
            tpl.setFontAndSize(pageTextFont, 12);
            tpl.setTextMatrix (0, 0);
            tpl.showText(" " + (writer.getPageNumber() - 2));
            tpl.setTextMatrix(380, 0);
            tpl.showText(DATE_FORMAT.format(new Date()));
            tpl.endText();
        }
    }
Off course the pa

Now what this does, is adding pageNumbers and a 'print-date' to my pdf-document and, in fact, it works fine.

The problem is, that it only does it on a document with multiple pages, as soon as I generate a doc with only one page, nothing shows up.

Any ideas?

Greetz,

Stijn

Reply via email to