You are creating the event too late. Add it to the writer before opening the doc or call onEndPage() instead of onStartPage().
 
Paulo 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Stijn Niclaes
Sent: Wednesday, May 24, 2006 3:05 PM
To: [email protected]
Subject: [iText-questions] PdfPageEventHelper problem

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


Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter informação confidencial ou legalmente protegida. A incorrecta transmissão desta mensagem não significa a perca de confidencialidade. Se esta mensagem for recebida por engano, por favor envie-a de volta para o remetente e apague-a do seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de usar, revelar ou distribuir qualquer parte desta mensagem.

Disclaimer:
This message is destined exclusively to the intended receiver. It may contain confidential or legally protected information. The incorrect transmission of this message does not mean the loss of its confidentiality. If this message is received by mistake, please send it back to the sender and delete it from your system immediately. It is forbidden to any person who is not the intended receiver to use, distribute or copy any part of this message.

Reply via email to