将itextsharp条形码图像添加到PdfPCell [英] Add itextsharp barcode image to PdfPCell

查看:947
本文介绍了将itextsharp条形码图像添加到PdfPCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying generate barcodes from a text string and then PDF the results using iTextSharp. Right now, I have the code below:

// 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时,我收到一条错误消息文档已包含没有页面。

When I run this and try to open the PDF programmatically, I receive an error saying "The document has no pages."

但是当我使用时:

 pdfToCreate.Add(image128);

(而不是添加到表格中,我将其添加到单元格中),条形码显示(虽然它从文档中浮出来。)

(instead of adding to a table, I add it to a cell), the barcode shows up (though it's floating off the document).

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

I'd like to send the barcodes to a table so that I can format the document easier. The final product will be 20-60 barcodes read from a database. Any idea on where I'm going wrong?

推荐答案

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

You are telling the PdfPTable constructor to create a three column table but only providing one of the columns:

PdfPTable BarCodeTable = new PdfPTable(3);

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

iTextSharp by default ignores incomplete rows. You can either change the constructor to match your column count, add the extra cells or call BarCodeTable.CompleteRow() which will fill in the blanks using the default cell template.

这篇关于将itextsharp条形码图像添加到PdfPCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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