在itext中向表中的单元格添加更多文本

时间:2014-08-15 11:10:23

标签: java itext barcode

我正在尝试使用itext按照以下代码在表格单元格中添加一些带条形码的文本,但它不会显示在pdf文件中。我尝试添加块和段。任何有关这方面的帮助将不胜感激。

Barcode128 barcode = new Barcode128();
//barcode.setCodeType(Barcode.EAN8);
barcode.setCode(code);
PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);

Paragraph paragraph = new Paragraph("Hello World"); 
cell.addElement(paragraph);

cell.setPadding(10);

1 个答案:

答案 0 :(得分:3)

您可能会对 text 复合模式感到困惑。

使用PdfPCell(Image)构造函数时,可以在 text 模式下创建一个单元格。随后对addElement(Element)的任何调用都会将单元格切换到复合模式,删除之前在构造函数中输入的所有内容。

您必须以这种方式更改代码:

PdfPCell cell = new PdfPCell();

Barcode128 barcode = new Barcode128();
barcode.setCode(code);
Image barcodeImage = barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY);
cell.addElement(barcodeImage);

Paragraph paragraph = new Paragraph("Hello World"); 
cell.addElement(paragraph);