PdfPTable 创建图像的额外单元格

PdfPTable creating extra cells of an image

我正在创建一个包含 pdf 的内存流。 pdf 应该有 4 个不同图像的 table 写在上面。相反,pdf 有一个 table 完全用第一张图片填写,第二页有正确的 4 张图片。它应该看起来像这样 Correct output

但是这张图片在上面 incorrect output

当 fourPerPage 传入 true 时发生错误。当传递 false 时,它​​可以正常工作并将 4 张图像放在没有 table.

的两页上

代码如下:

    public Dictionary<string, MemoryStream> CreateW2FromBitmapImage2Page(List<Employee> info, Employer employerInfo, bool fourPerPage)
    {
        StringFormat rightAlign = new StringFormat(StringFormatFlags.NoClip);
        PdfPTable imageTable = new PdfPTable(2) { WidthPercentage = 82 };

        imageTable.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
        imageTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
        rightAlign.Alignment = StringAlignment.Far;

        var brushColor = Brushes.Black;
        var W2ImageDictionary = new Dictionary<string, MemoryStream>();
        var font = new System.Drawing.Font("Courier", 12);
        var document = new Document(PageSize.LETTER, 0, 0, 0, 0);

        foreach (var property in info)
        {
            Bitmap bmp = new Bitmap(Bitmap.FromFile("copy2.bmp"));
            MemoryStream tempImageStream = new MemoryStream();

            var writer = PdfWriter.GetInstance(document, tempImageStream);
            var employeeCityStateZipCode = CityStateZipDelimiter(property.CityStateZip);
            var employerCityStateZipCode = CityStateZipDelimiter(employerInfo.CityStateZip);

            document.Open();
            writer.CloseStream = false;

            for (int i = 1; i < 5; i++)
            {
                switch (i)
                {
                    case 1:
                        bmp = new Bitmap(Bitmap.FromFile("copy2.bmp"));
                        break;
                    case 2:
                        bmp = new Bitmap(Bitmap.FromFile("copy2.bmp"));
                        break;
                    case 3:
                        bmp = new Bitmap(Bitmap.FromFile("copyB.bmp"));
                        break;
                    case 4:
                        bmp = new Bitmap(Bitmap.FromFile("copyC.bmp"));
                        break;
                }

                Graphics g = Graphics.FromImage(bmp);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                #region formatW2
                //left col
                g.DrawString(employerInfo.EIN, font, brushColor, 10, 51);
                g.DrawString(employerInfo.Name, font, brushColor, 10, 92);
                g.DrawString(employerInfo.AddressName, font, brushColor, 10, 112);
                g.DrawString(employerInfo.ControlNumber, font, brushColor, 10, 178);
                g.DrawString(employerCityStateZipCode.Item1 + ", " + employerCityStateZipCode.Item2 + " " + employerCityStateZipCode.Item3, font, brushColor, 10, 130);
                g.DrawString(property.EmployeeSSN, font, brushColor, 185, 20);
                g.DrawString(property.EmployeeName, font, brushColor, 10, 215);
                g.DrawString(property.AddressName, font, brushColor, 10, 235);
                g.DrawString(employeeCityStateZipCode.Item1 + ", " + employeeCityStateZipCode.Item2 + " " + employeeCityStateZipCode.Item3, font, brushColor, 10, 255);
                //middle col
                g.DrawString(property.SocialSecurityWages.ToString(), font, brushColor, 550, 83, rightAlign);
                g.DrawString(property.SocialSecurityTips.ToString(), font, brushColor, 550, 147, rightAlign);
                g.DrawString(property.MedicareWages.ToString(), font, brushColor, 550, 115, rightAlign);
                g.DrawString(property.GrossWages.ToString(), font, brushColor, 550, 51, rightAlign);
                //g.DrawString(property.verificationcode, font, brushColor, 550, 179, rightAlign);
                g.DrawString(property.NonqualifiedPlans, font, brushColor, 550, 211, rightAlign);
                g.DrawString(property.RetirementPlan.ToString(), font, brushColor, 470, 246);
                //g.DrawString("X", font, brushColor, 422, 246);
                //g.DrawString("X", font, brushColor, 519, 246);
                //right col
                g.DrawString(property.FederalTaxWithheld.ToString(), font, brushColor, 715, 51, rightAlign);
                g.DrawString(property.SocialSecurityTaxWithheld.ToString(), font, brushColor, 715, 83, rightAlign);
                g.DrawString(property.MedicareTaxWithheld.ToString(), font, brushColor, 715, 115, rightAlign);
                g.DrawString(property.AllocatedTips.ToString(), font, brushColor, 715, 147, rightAlign);
                g.DrawString(property.DependentCareBenefits, font, brushColor, 715, 179, rightAlign);
                g.DrawString(property.TwelveARight.ToString(), font, brushColor, 715, 211, rightAlign);
                g.DrawString(property.TwelveALeft, font, brushColor, 605, 211, rightAlign);
                g.DrawString(property.TwelveBRight.ToString(), font, brushColor, 715, 243, rightAlign);
                g.DrawString(property.TwelveBLeft, font, brushColor, 605, 243, rightAlign);
                g.DrawString(property.TwelveCRight.ToString(), font, brushColor, 715, 275, rightAlign);
                g.DrawString(property.TwelveCLeft, font, brushColor, 605, 275, rightAlign);
                g.DrawString(property.TwelveDRight.ToString(), font, brushColor, 715, 307, rightAlign);
                g.DrawString(property.TwelveDLeft, font, brushColor, 605, 307, rightAlign);
                //bottom row
                g.DrawString(employerInfo.State, font, brushColor, 12, 355);
                g.DrawString(employerInfo.SIN, font, brushColor, 55, 355);
                g.DrawString(property.StateWages.ToString(), font, brushColor, 228, 355);
                g.DrawString(property.StateIncomeTax.ToString(), font, brushColor, 345, 355);
                g.DrawString(property.LocalWages.ToString(), font, brushColor, 456, 355);
                g.DrawString(property.LocalIncomeTax.ToString(), font, brushColor, 571, 355);
                g.DrawString(property.LocalityName, font, brushColor, 676, 355);
                g.Flush();
                #endregion formatW2

                if (fourPerPage == true)
                {
                    bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);

                    try
                    {
                        iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(bmp, ImageFormat.Bmp);


                        if (pic.Height > pic.Width)
                        {
                            float percentage = 0f;
                            percentage = 400 / pic.Height;
                            pic.ScalePercent(percentage * 100);
                        }
                        else
                        {
                            float percentage = 0f;
                            percentage = 240 / pic.Width;
                            pic.ScalePercent(percentage * 100);
                        }
                        pic.Border = iTextSharp.text.Rectangle.BOX;
                        pic.BorderColor = BaseColor.BLACK;
                        pic.BorderWidth = 3f;
                        imageTable.AddCell(pic);


                        document.Add(imageTable);
                        g.Dispose();
                        //document.Close();
                        //document.Open();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(bmp, ImageFormat.Bmp);
                    pic.ScalePercent(80);
                    document.Add(pic);
                    g.Dispose();
                }
            }
            document.Close();
            W2ImageDictionary.Add(property.EmployeeSSN, tempImageStream);
        }

        return W2ImageDictionary;
    }

你打电话

document.Add(imageTable);

for 循环中。因此,您将 table 添加到文档 四次 !

第一次只有一个单元格,即没有完整的行,什么都不画。

第二次有两个单元格,即一个完整的行,所以画了一行。

Copy 2 | Copy 2

第三次是三个单元格,还是只有一个完整的行,所以重新画第一行。

Copy 2 | Copy 2
Copy 2 | Copy 2

第四次有四个单元格,即完整的两行,现在整个table画完了。

Copy 2 | Copy 2
Copy 2 | Copy 2
Copy 2 | Copy 2
Copy B | Copy C

尝试仅在最后一次迭代中调用该行,即

if (i == 4)
    document.Add(imageTable);

现在 table 应该只绘制一次,只有在完成时才绘制。