Add the itextsharp barcode image to PdfPCell

I am trying to generate barcodes from a text string and then PDF 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();

      

When I run this and try to open the PDF programmatically, I get the "Document has no pages" error.

However, when I use:

 pdfToCreate.Add(image128);

      

(instead of adding it to the table, I add it to the cell) the barcode appears (although it gets dropped from the document).

I would like to send barcodes to the table to make formatting the document easier. The end product will be 20-60 barcodes read from the database. Any idea where I am going wrong?

0


source to share


1 answer


You are telling the constructor PdfPTable

to create a three-column table, but providing only one of the columns:

PdfPTable BarCodeTable = new PdfPTable(3);

      



iTextSharp ignores incomplete lines by default. You can either change the constructor to suit the number of columns, add additional cells, or call BarCodeTable.CompleteRow()

that fills in the blanks using the default cell template.

+3


source







All Articles