Doc H
Doc H

Reputation: 43

Create a Barcode Overlay on a PDF with an instransparent Background (iText)

Currently i adding an Barcode with the following code:

PdfContentByte overContent = pdfStamper.getOverContent(page);
Rectangle pageSize = pdfReader.getPageSize(page);

Barcode128 code128 = new Barcode128();
code128.setCode(barcodeText);
code128.setCodeType(Barcode128.CODE128);
PdfTemplate template = code128.createTemplateWithBarcode(overContent, BaseColor.BLACK, BaseColor.BLACK);

Rectangle barcodeSize = code128.getBarcodeSize();

float x = pageSize.getRight() - barcodeSize.getWidth() - 10;
float y = pageSize.getTop() - barcodeSize.getHeight() - 10;

overContent.addTemplate(template, x, y);

Unfortunately, the resulting stamped image has an transparent Background. The Content behind the Barcode disturbs Scanner-Software:

https://i.sstatic.net/STOsp.jpg

Alternative i can add an Barcode with

Image image = code128.createImageWithBarcode(over, BaseColor.BLACK, BaseColor.BLACK);
image.setAbsolutePosition(x, y);
over.addImage(image);

But there, the setTransparency Method seems do do nothing.

Anyone has an hint for me how to create a Barcode on a plain white Background?

Edit - Solution:

PdfContentByte overContent = pdfStamper.getOverContent(page);
Rectangle pageSize = pdfReader.getPageSize(page);

Barcode128 code128 = new Barcode128();
code128.setCode(barcodeText);
code128.setCodeType(Barcode128.CODE128);

PdfTemplate template = code128.createTemplateWithBarcode(overContent, BaseColor.BLACK, BaseColor.BLACK);

float x = pageSize.getRight() - template.getWidth() - 10;
float y = pageSize.getTop() - template.getHeight() - 10;
float w = pageSize.getRight() - 10;
float h = pageSize.getTop() - 10;

overContent.saveState();
overContent.setColorFill(BaseColor.WHITE);
overContent.rectangle(x-2, y-2, w+4, h+4);
overContent.fill();
overContent.restoreState();

overContent.addTemplate(template, x, y);

Upvotes: 4

Views: 1565

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77546

Please take a look at the BarcodeBackground. It adds a bar code to a document created from scratch as well as a light gray background:

enter image description here

You'll have to adapt this to adding a bar code to an already existing document. Instead of a light gray background, you can easily use an opaque white background. That shouldn't be a problem, because the principle is the same:

I copied the code you used to create a bar code:

PdfContentByte canvas = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCode("12345XX789XXX");
code128.setCodeType(Barcode128.CODE128);
 PdfTemplate template = code128.createTemplateWithBarcode(
        canvas, BaseColor.BLACK, BaseColor.BLACK);

I use hard coded coordinates and calculated the width and height of the bar code:

float x = 36;
float y = 750;
float w = template.getWidth();
float h = template.getHeight();

Before adding the bar code, I add a rectangle in a specific color. Note that I use a saveState()/restoreState() sequence to make sure that the fill color is restored to the original color once the rectangle is filled:

canvas.saveState();
canvas.setColorFill(BaseColor.LIGHT_GRAY);
canvas.rectangle(x, y, w, h);
canvas.fill();
canvas.restoreState();

Once the rectangle is drawn, you can add the bar code:

canvas.addTemplate(template, 36, 750);

Upvotes: 2

Related Questions