我正在创建一个带有 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;
}