user1579878
user1579878

Reputation: 107

iText 7 Adding barcode to every page with an event

I need to add a barcode to every page of the iText Pdf so I thought an event might be what I need but my following code puts the barcode only on the last page.

BarcodeEventHandler barCodeHandler = new  BarcodeEventHandler(pdfDoc, doc);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, barCodeHandler);

protected class BarcodeEventHandler implements IEventHandler {
    protected PdfDocument Pdfdoc;
    protected Document doc;

    public BarcodeEventHandler(PdfDocument Pdfdoc, Document doc) {
        this.Pdfdoc = Pdfdoc;
        this.doc = doc;
    }

    @Override
    public void handleEvent(Event event) {
        Barcode128 barcode = new Barcode128(Pdfdoc);
        barcode.setCodeType(Barcode128.CODE128);
        barcode.setCode("12345678");
        barcode.setSize(12);
        Rectangle rect = barcode.getBarcodeSize();
        PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(rect.getWidth(), rect.getHeight() + 10));
        PdfCanvas pdfCanvas = new PdfCanvas(formXObject, Pdfdoc);
        barcode.placeBarcode(pdfCanvas, ColorConstants.BLACK, ColorConstants.BLACK);
        Image bCodeImage = new Image(formXObject);
        bCodeImage.setRotationAngle(Math.toRadians(90));        
        bCodeImage.setFixedPosition(100,200);       
        doc.add(bCodeImage);
    }
}

Is there any other way I can achive this. Thanks.

Ron

Upvotes: 1

Views: 871

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12302

You can get information about current page being flushed from event by casting it to PdfDocumentEvent since you are registering your event handler for a specific event type. Also, you don't need to pass Document to your handler at all. After getting PdfPage from the event you can easily add your barcode image to that page with Canvas helper class. Handler code:

protected class BarcodeEventHandler implements IEventHandler {
    protected PdfDocument Pdfdoc;

    public BarcodeEventHandler(PdfDocument Pdfdoc) {
        this.Pdfdoc = Pdfdoc;
    }

    @Override
    public void handleEvent(Event event) {
        Barcode128 barcode = new Barcode128(Pdfdoc);
        barcode.setCodeType(Barcode128.CODE128);
        barcode.setCode("12345678");
        barcode.setSize(12);
        Rectangle rect = barcode.getBarcodeSize();
        PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(rect.getWidth(), rect.getHeight() + 10));
        PdfCanvas pdfCanvas = new PdfCanvas(formXObject, Pdfdoc);
        barcode.placeBarcode(pdfCanvas, ColorConstants.BLACK, ColorConstants.BLACK);
        Image bCodeImage = new Image(formXObject);
        bCodeImage.setRotationAngle(Math.toRadians(90));
        bCodeImage.setFixedPosition(100,200);

        PdfPage page = ((PdfDocumentEvent)event).getPage();
        new Canvas(page, PageSize.A4.clone()).add(bCodeImage);
    }
}

Registering event handler (be sure to do so before adding content to the document):

BarcodeEventHandler barCodeHandler = new  BarcodeEventHandler(pdfDocument);
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, barCodeHandler);

Upvotes: 1

Related Questions