我正在尝试从文本字符串生成条形码,然后使用 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 个条形码。知道我哪里出错了吗?
最佳答案
您告诉 PdfPTable
构造函数创建一个三列表,但只提供其中一列:
PdfPTable BarCodeTable = new PdfPTable(3);
iTextSharp 默认情况下会忽略不完整的行。您可以更改构造函数以匹配您的列数、添加额外的单元格或调用 BarCodeTable.CompleteRow()
这将使用默认单元格模板填充空白。
关于itext - 将 itextsharp 条形码图像添加到 PdfPCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19412948/