Magobin
Magobin

Reputation: 103

Print to Pdf...only last 2 pages

I have problem with my code. Once I have my pdf page correctly created, using an example of ItextPdf 7 I want to print for every page the number of page as Page x of Y....the code seems runs correctly, but only the last 2 pages are correctly printed.

Thanks in advance Alex

below my code:

    .....snippet code for document creation !

    PageXofY event = new PageXofY(pdf);
    pdf.addEventHandler(PdfDocumentEvent.END_PAGE, event) ;
    event.writeTotal(pdf);
    document.close();

e this is the class that I use to print:

protected class PageXofY implements IEventHandler {
protected PdfFormXObject placeholder;
protected float side = 20;
protected float x = 300;
protected float y = 25;
protected float space = 4.5f;
protected float descent = 3;

public PageXofY(PdfDocument pdf) {
    placeholder =  new PdfFormXObject(new Rectangle(0, 0, side, side));

}
@Override
public void handleEvent(Event event) {
    PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
    PdfDocument pdf = docEvent.getDocument();
    PdfPage page = docEvent.getPage();

    int pageNumber = pdf.getPageNumber(page);

    Rectangle pageSize = page.getPageSize();
    PdfCanvas pdfCanvas = new PdfCanvas(
        page.getLastContentStream(), page.getResources(), pdf);
    Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
    Paragraph p = new Paragraph()
        .add("Pagina ").add(String.valueOf(pageNumber)).add(" di");
    canvas.showTextAligned(p, x, y, TextAlignment.RIGHT);
    pdfCanvas.addXObject(placeholder, x + space, y - descent);
    pdfCanvas.release();
}
public void writeTotal(PdfDocument pdf) {
    Canvas canvas = new Canvas(placeholder, pdf);
    canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
        0, descent, TextAlignment.LEFT);
}

Upvotes: 0

Views: 356

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

It looks as if these two lines are at the end of your code:

// add all the content here
PageXofY event = new PageXofY(pdf);
pdf.addEventHandler(PdfDocumentEvent.END_PAGE, event) ;
event.writeTotal(pdf);
document.close();

This means that the event only becomes active after you've already added most of your content, which explains why the footer is added only for the pages at the end of your document.

Please change your code like this:

PageXofY event = new PageXofY(pdf);
pdf.addEventHandler(PdfDocumentEvent.END_PAGE, event) ;
// add all the content here
event.writeTotal(pdf);
document.close();

I am the author of the example you used for inspiration. If you look closely at the original PageXofY example, you'll see that I add the event handler even before creating the Document instance:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PageXofY event = new PageXofY(pdf);
pdf.addEventHandler(PdfDocumentEvent.END_PAGE, event);
Document document = new Document(pdf);
// add all the content
event.writeTotal(pdf);
document.close();

If you read the full chapter, you'll understand how it works. In another example (see the ImageWatermark example), I remove the event handler:

pdf.removeEventHandler(PdfDocumentEvent.START_PAGE, handler);

This causes the event not to be triggered for the last page.

Upvotes: 1

Related Questions