Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...