How to display barcodes in a matrix-like structure?

喜夏-厌秋 提交于 2019-12-13 08:08:21

问题


How can I display different barcodes in multiple columns in a PDF page using itext library to generate pdfs in java? I have to display 12 barcodes in the same PDF page in three columns, each one contains 4 barcodes (in other words it is a 4 by 3 matrix).


回答1:


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 you add 12 cell:

PdfPTable table = new PdfPTable(4);
table.setWidthPercentage(100);
for (int i = 0; i < 12; i++) {
    table.addCell(createBarcode(writer, String.format("%08d", i)));
}

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

public static PdfPCell createBarcode(PdfWriter writer, String code) throws DocumentException, IOException {
    BarcodeEAN barcode = new BarcodeEAN();
    barcode.setCodeType(Barcode.EAN8);
    barcode.setCode(code);
    PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);
    cell.setPadding(10);
    return cell;
}


来源:https://stackoverflow.com/questions/21981602/how-to-display-barcodes-in-a-matrix-like-structure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!