How to display barcodes in a matrix-like structure?

Tags: bar codetablesimage in celliText 7

How can I display different bar codes in multiple columns in a PDF page using iText? I have to display 12 bar codes in the same PDF page in three columns each one containing 4 bar codes (in other words it's 4 by 3 matrix).

Posted on StackOverflow on Feb 24, 2014 by user3323182

I've made a Barcodes example that does exactly what you need. See the resulting pdf: barcodes_table.pdf

There's nothing difficult about it. You just create a table with 4 column and add 12 cells:

Table table = new Table(4);
for (int i = 0; i < 12; i++) {
    table.addCell(createBarcode(String.format("%08d", i), pdfDoc));

The createBarcode() method creates a cell with a barcode:

public static Cell createBarcode(String code, PdfDocument pdfDoc) {
    BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
    Cell cell = new Cell()
            .add(new Image(barcode.createFormXObject(null, null, pdfDoc)))
    return cell;

Note that there are no PdfPTables and PdfPCells in iText 7, we use Tables and Cellsinstead.

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