gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:21:18 26 4
gpt4 key购买 nike

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

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);

最佳答案

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

当使用 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);

关于java - 在 itext 中的表格单元格中添加更多文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com