Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

asp.net - Odd Numbered Cell Not Added To Pdf

I am trying to add PdfPCell inside a loop to a iTextSharp Table with 2 columns in a Document. But if the count inside the loop is an odd number. Then the last cell does not get added. Can someone please provide a solution to this problem? My code is below:

        var doc = new Document();
        PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~/QrCodes/") + fileName + ".pdf", FileMode.Create));
        doc.Open();
        PdfPTable table = new PdfPTable(2);
        table.WidthPercentage = 100;

        foreach (var item in items)
        {
            if (itemImages.Any(p => p.Reference == item.Reference) == true)
            {
                System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(@item.ItemQrCode));
                iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, ImageFormat.Jpeg);

                PdfPCell cellImage = new PdfPCell(pdfImage);
                cellImage.HorizontalAlignment = Element.ALIGN_CENTER;
                cellImage.VerticalAlignment = Element.ALIGN_MIDDLE;
                cellImage.Border = 0;

                table.AddCell(cellImage);

            }

        }

        doc.Add(table);
        doc.Close();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On your PdfPTable you can call the CompleteRow() method when you're done and missing cells will be filled in.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...