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

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...