How to put text above a bar code instead of under the bars?

Tags: bar codetablesiText 7

I'm using iText to dynamically generate PDF docs.I'm able to introduce bar codes with text below the bars like this:

Bar code with text below the bars

I need the text to be above the bars as shown in this screen shot:

Bar code with text above the bars

How can I do this?

Question posted on StackOverflow on Jan 26, 2016 by girish

Please take a look at the BarcodeInTable example. In this example, we add a bar code in two different ways:

Two ways to add text above the bars in a bar code
Two ways to add text above the bars in a bar code

In the first way, we use the setBaseLine() method with a negative value to make sure the code is added above the bar code instead of under it:

Barcode128 code128 = new Barcode128(pdfDoc);
Image code128Image = new Image(code128.createFormXObject(pdfDoc));
Cell cell = new Cell().add(code128Image);

You may want to have more freedom with respect to the text you are adding. For instance: you may want to add PO #: as is done in your example.

That's why I also provided a second way to add the text and the bar code:

code128 = new Barcode128(pdfDoc);
code128Image = new Image(code128.createFormXObject(pdfDoc)).setAutoScale(true);
cell = new Cell();
cell.add("PO #: " + code);

In this case, we set the font to null so that no text is generated. We add the text to the cell, along with the bar code. Now we have much more freedom to format the text exactly the way we want it.

Click this link if you want to see how to answer this question in iText 5.