iText 8 – PdfDocumentEvent.END_PAGE not getting fired

  Kiến thức lập trình

It looks like PdfDocumentEvent.END_PAGE is not getting fired all the times as expected. The code below will generate C:temppageEvent.pdf, and if you look at page 5 or page 9, they have new page, and header is the first line on the new page, and I want zero top-padding there.

package com.falconjet.molp;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.FileNotFoundException;

public class PageEventDemo {
    public void createPdf() throws FileNotFoundException {
        PdfWriter writer = new PdfWriter("C:\temp\pageEvent.pdf");
        PdfDocument pdf = new PdfDocument(writer);
        NewPageEventHandler newPageEventHandler = new NewPageEventHandler();
        pdf.addEventHandler(PdfDocumentEvent.END_PAGE, newPageEventHandler);
        Document document = new Document(pdf);
        document.setMargins(0, 5, 0, 5);
        Table table = new Table(UnitValue.createPointArray(new float[]{100}));
        table.setWidth(UnitValue.createPercentValue(100));
        for(int i = 0; i < 200; ++i) {
            if(i % 5 == 0) {
                float topPadding = newPageEventHandler.getNoTopPadding() ? 0 : 15f;
                Cell cell = new Cell()
                        .setPaddingTop(topPadding)
                        .setBorder(Border.NO_BORDER)
                        .setBold()
                        .add(new Paragraph("Header"));
                table.addCell(cell);
            } else if (i % 2 == 0) {
                Cell cell = new Cell()
                        .setPaddingTop(0)
                        .setBorder(Border.NO_BORDER)
                        .add(new Paragraph("Small data cell"));
                table.addCell(cell);
            } else {
                Cell cell = new Cell()
                        .setPaddingTop(0)
                        .setBorder(Border.NO_BORDER)
                        .add(new Paragraph("Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell Long data cell "));
                table.addCell(cell);
            }
        }
        document.add(table);
        pdf.close();
    }
    class NewPageEventHandler implements IEventHandler {
        boolean noTopPadding;
        @Override
        public void handleEvent(Event event) {
            //No top padding if header is the first line on a new page
            noTopPadding = true;
        }
        boolean getNoTopPadding() {
            return noTopPadding;
        }
    }
}

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT