在itext中向表中的单元格添加更多文本 [英] Adding more text to a cell in table in itext

查看:1003
本文介绍了在itext中向表中的单元格添加更多文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add some text with a bar code in a table cell using itext as per the following code but it does not show in the pdf file. i tried adding chunks and paragraph. Any help on this would be appreciated.

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


推荐答案

您可能对文本感到困惑 vs. 复合模式。

You are probably confused by text vs. composite mode.

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

When using the PdfPCell(Image) constructor, you create a cell in text mode. Any subsequent call to addElement(Element) will then switch the cell to composite mode, removing all content previously entered in the constructor.

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

You'll have to change your code that way:

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

这篇关于在itext中向表中的单元格添加更多文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆