【问题标题】:Adding more text to a cell in table in itext在 itext 中向表格中的单元格添加更多文本
【发布时间】:2014-08-15 20:38:49
【问题描述】:

我正在尝试按照以下代码使用 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);

【问题讨论】:

  • 当您不是在cell 的构造函数中添加条形码,而是通过cell.addElement() 调用时,它不起作用吗?

标签: java itext barcode


【解决方案1】:

您可能对 textcomposite 模式感到困惑。

当使用PdfPCell(Image) 构造函数时,您将在文本 模式下创建一个单元格。随后对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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多