Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PdfPTable创建图像的额外单元格_C#_Itext_Pdfptable - Fatal编程技术网

C# PdfPTable创建图像的额外单元格

C# PdfPTable创建图像的额外单元格,c#,itext,pdfptable,C#,Itext,Pdfptable,我正在创建一个包含pdf的内存流。pdf应该有一个包含4个不同图像的表格,这些图像都写在上面。相反,pdf有一个表格完全填写了第一张图片,第二页有正确的4张图片。应该是这样的 但相反,这张图片就在上面 当fourPerPage传递为true时,会发生错误。当通过false时,它工作正常,将4个图像放在两页上,没有表格 以下是代码: public Dictionary<string, MemoryStream> CreateW2FromBitmapImage2Page(L

我正在创建一个包含pdf的内存流。pdf应该有一个包含4个不同图像的表格,这些图像都写在上面。相反,pdf有一个表格完全填写了第一张图片,第二页有正确的4张图片。应该是这样的

但相反,这张图片就在上面

当fourPerPage传递为true时,会发生错误。当通过false时,它工作正常,将4个图像放在两页上,没有表格

以下是代码:

    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;
    }
public Dictionary CreateW2FromBitmapImage2Page(列表信息,雇主信息,bool fourPerPage)
{
StringFormat rightAlign=新的StringFormat(StringFormatFlags.NoClip);
PdfPTable imageTable=新PdfPTable(2){宽度百分比=82};
imageTable.DefaultCell.Border=iTextSharp.text.Rectangle.NO_Border;
imageTable.DefaultCell.HorizontalAlignment=Element.ALIGN\u CENTER;
rightAlign.Alignment=StringAlignment.Far;
var brushColor=画笔。黑色;
var W2ImageDictionary=新字典();
var font=新系统.Drawing.font(“Courier”,12);
var文档=新文档(PageSize.LETTER,0,0,0);
foreach(信息中的var属性)
{
位图bmp=新位图(Bitmap.FromFile(“copy2.bmp”);
MemoryStream tempImageStream=新的MemoryStream();
var writer=PdfWriter.GetInstance(文档,tempImageStream);
var employeeCityStateZipCode=CityStateZipDelimiter(property.CityStateZip);
var employerCityStateZipCode=CityStateZipDelimiter(employerInfo.CityStateZip);
document.Open();
writer.CloseStream=false;
对于(int i=1;i<5;i++)
{
开关(一)
{
案例1:
bmp=新位图(Bitmap.FromFile(“copy2.bmp”);
打破
案例2:
bmp=新位图(Bitmap.FromFile(“copy2.bmp”);
打破
案例3:
bmp=新位图(Bitmap.FromFile(“copyB.bmp”);
打破
案例4:
bmp=新位图(Bitmap.FromFile(“copyC.bmp”);
打破
}
Graphics g=Graphics.FromImage(bmp);
g、 SmoothingMode=SmoothingMode.AntiAlias;
g、 PixelOffsetMode=PixelOffsetMode.HighQuality;
g、 插值模式=插值模式。高质量双三次;
#区域格式W2
//左列
g、 抽绳(employerInfo.EIN,字体,刷色,10,51);
g、 抽绳(employerInfo.Name、字体、笔刷颜色、10、92);
g、 抽绳(employerInfo.AddressName,字体,刷色,10112);
g、 抽绳(employerInfo.ControlNumber,字体,笔刷颜色,10178);
g、 抽绳(employerCityStateZipCode.Item1+”、“+employerCityStateZipCode.Item2+”、“+employerCityStateZipCode.Item3,字体,笔刷颜色,10130);
g、 抽绳(property.EmployeeSSN,字体,笔刷颜色,185,20);
g、 抽绳(property.EmployeeName,font,brushColor,10215);
g、 抽绳(property.AddressName,font,brushColor,10235);
g、 抽绳(employeeCityStateZipCode.Item1+”,“+employeeCityStateZipCode.Item2+”+employeeCityStateZipCode.Item3,字体,画笔颜色,10255);
//中柱
g、 DrawString(property.SocialSecurityWages.ToString(),字体,brushColor,550,83,rightAlign);
g、 DrawString(property.SocialSecurityTips.ToString(),字体,brushColor,550147,rightAlign);
g、 DrawString(property.MedicareWages.ToString(),字体,brushColor,550,115,rightAlign);
g、 DrawString(property.GrossWages.ToString(),字体,brushColor,550,51,rightAlign);
//g、 抽绳(property.verificationcode、字体、brushColor、550、179、rightAlign);
g、 抽绳(property.NonqualifiedPlans,字体,brushColor,550,211,rightAlign);
g、 DrawString(property.RetirementPlan.ToString(),字体,brushColor,470246);
//g、 抽绳(“X”,字体,刷色,422246);
//g、 抽绳(“X”,字体,刷色,519246);
//右柱
g、 DrawString(property.FederalTaxInjected.ToString(),字体,brushColor,715,51,rightAlign);
g、 DrawString(property.socialsecuritytaxpreserved.ToString(),字体,brushColor,715,83,rightAlign);
g、 DrawString(property.medicartaxpreserved.ToString(),字体,brushColor,715115,rightAlign);
g、 DrawString(property.AllocatedTips.ToString(),字体,brushColor,715147,rightAlign);
g、 抽绳(property.dependentCarbenefits、字体、brushColor、715179、rightAlign);
g、 DrawString(property.tweevaright.ToString(),字体,brushColor,715211,rightAlign);
g、 抽绳(property.tweevaleft,font,brushColor,605,211,rightAlign);
g、 DrawString(property.TwelveBRight.ToString(),字体,brushColor,715243,rightAlign);
g、 抽绳(property.twevelbleft,font,brushColor,605243,rightAlign);
g、 DrawString(property.TwelveCRight.ToString(),字体,brushColor,715275,rightAlign);
g、 抽绳(property.TwelveClaft,字体,brushColor,605275,rightAlign);
g、 DrawString(property.TwelveDRight.ToString(),字体,brushColor,715307,rightAlign);
g、 抽绳(property.TwelveDLeft,字体,brushColor,605,307,rightAlign);
//底层
g、 抽绳(employerInfo.State,字体,笔刷颜色,12355);
g、 抽绳(employerInfo.SIN、字体、刷色
document.Add(imageTable);
Copy 2 | Copy 2
Copy 2 | Copy 2
Copy 2 | Copy 2
Copy 2 | Copy 2
Copy 2 | Copy 2
Copy 2 | Copy 2
Copy B | Copy C
if (i == 4)
    document.Add(imageTable);