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
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); code128.setBaseline(-1); code128.setSize(12); code128.setCode(code); code128.setCodeType(Barcode128.CODE128); Image code128Image = new Image(code128.createFormXObject(pdfDoc)); Cell cell = new Cell().add(code128Image); table.addCell(cell);
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); code128.setFont(null); code128.setCode(code); code128.setCodeType(Barcode128.CODE128); code128Image = new Image(code128.createFormXObject(pdfDoc)).setAutoScale(true); cell = new Cell(); cell.add("PO #: " + code); cell.add(code128Image); table.addCell(cell);
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.