【问题标题】:Add itextsharp barcode image to PdfPCell将 itextsharp 条码图像添加到 PdfPCell
【发布时间】:2013-10-16 20:14:37
【问题描述】:

我正在尝试从文本字符串生成条形码,然后使用 iTextSharp 将结果生成 PDF。现在,我有以下代码:

// Create a Document object
var pdfToCreate = new Document(PageSize.A4, 0, 0, 0, 0);

// Create a new PdfWrite object, writing the output to a MemoryStream
var outputStream = new MemoryStream();
var pdfWriter = PdfWriter.GetInstance(pdfToCreate, outputStream);
PdfContentByte cb = new PdfContentByte(pdfWriter);
// Open the Document for writing
pdfToCreate.Open();

PdfPTable BarCodeTable = new PdfPTable(3);
// Create barcode
Barcode128 code128          = new Barcode128();
code128.CodeType            = Barcode.CODE128_UCC;
code128.Code                = "00100370006756555316";
// Generate barcode image
iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
// Add image to table cell
BarCodeTable.AddCell(image128);
// Add table to document
pdfToCreate.Add(BarCodeTable);

pdfToCreate.Close();

当我运行此程序并尝试以编程方式打开 PDF 时,我收到一条错误消息,提示“文档没有页面。”

但是当我使用时:

 pdfToCreate.Add(image128);

(我没有将其添加到表格中,而是将其添加到单元格中),条形码会显示出来(尽管它漂浮在文档之外)。

我想将条形码发送到表格,以便更轻松地格式化文档。最终产品将是从数据库中读取的 20-60 个条形码。知道我哪里出错了吗?

【问题讨论】:

    标签: itextsharp cell barcode


    【解决方案1】:

    您告诉PdfPTable 构造函数创建一个三列表,但只提供其中一列:

    PdfPTable BarCodeTable = new PdfPTable(3);
    

    iTextSharp 默认忽略不完整的行。您可以更改构造函数以匹配您的列数,添加额外的单元格或调用BarCodeTable.CompleteRow(),它将使用默认单元格模板填充空白。

    【讨论】:

    • 就是这样!非常感谢,我猜我太着急看条形码了。
    猜你喜欢
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2013-11-11
    相关资源
    最近更新 更多