我试图创建一个包含以下内容的两页PDF:1)一页上有2x2 W2,这部分是正确的; 2)(新)第一页的说明,这部分不起作用。
目前这段代码运行正常,但我节省的时间是2x2 W2的初始PDF。我觉得我缺少一些逻辑/转换(由于表)或流可能太大。
流创建:
public Dictionary<string, MemoryStream> CreateW2FromBitmap(List<Employee> info, Employer employerInfo, bool fourPerPage)
{
StringFormat rightAlign = new StringFormat(StringFormatFlags.NoClip);
StringFormat centerAlign = 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;
centerAlign.Alignment = StringAlignment.Center;
var brushColor = Brushes.Black;
var W2ImageDictionary = new Dictionary<string, MemoryStream>();
var font = new System.Drawing.Font("Courier New", 47);
var document = new Document(PageSize.LETTER, 0, 0, 10, 0);
Bitmap bmp = new Bitmap(Bitmap.FromFile(@"\\ProdFS\DocumentArchive$\MessagingService\W2\W2_Images_For_Creation\Copy-2_" + employerInfo.taxYear + ".png"));
foreach (var property in info)
{
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(@"\\ProdFS\DocumentArchive$\MessagingService\W2\W2_Images_For_Creation\Copy-2_" + employerInfo.taxYear + ".png"));
break;
case 2:
bmp = new Bitmap(Bitmap.FromFile(@"\\ProdFS\DocumentArchive$\MessagingService\W2\W2_Images_For_Creation\Copy-2_" + employerInfo.taxYear + ".png"));
break;
case 3:
bmp = new Bitmap(Bitmap.FromFile(@"\\ProdFS\DocumentArchive$\MessagingService\W2\W2_Images_For_Creation\Copy-B_" + employerInfo.taxYear + ".png"));
break;
case 4:
bmp = new Bitmap(Bitmap.FromFile(@"\\ProdFS\DocumentArchive$\MessagingService\W2\W2_Images_For_Creation\Copy-C_" + employerInfo.taxYear + ".png"));
break;
}
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//left col
g.DrawString(property.EmployeeSSN, font, brushColor, 765, 90, centerAlign);
g.DrawString(employerInfo.EIN, font, brushColor, 30, 190);
g.DrawString(employerInfo.Name, font, brushColor, 30, 322);
g.DrawString(employerInfo.AddressName, font, brushColor, 30, 392);
g.DrawString(employerCityStateZipCode.Item1 + ", " + employerCityStateZipCode.Item2 + " " + employerCityStateZipCode.Item3, font, brushColor, 30, 462);
//g.DrawString(employerInfo.ControlNumber, font, brushColor, 30, 590);
g.DrawString(property.EmployeeName, font, brushColor, 30, 715);
g.DrawString(property.AddressName, font, brushColor, 30, 820);
g.DrawString(employeeCityStateZipCode.Item1 + ", " + employeeCityStateZipCode.Item2 + " " + employeeCityStateZipCode.Item3, font, brushColor, 30, 890);
//middle col
g.DrawString(property.GrossWages, font, brushColor, 1765, 190, rightAlign);
g.DrawString(property.SocialSecurityWages, font, brushColor, 1765, 290, rightAlign);
g.DrawString(property.MedicareWages, font, brushColor, 1765, 390, rightAlign);
//g.DrawString(property.SocialSecurityTips, font, brushColor, 1765, 514, rightAlign);
//g.DrawString(property.verificationcode, font, brushColor, 1765, 626.5, rightAlign);
//g.DrawString(property.NonqualifiedPlans, font, brushColor, 1765, 750, rightAlign);
//g.DrawString("X", font, brushColor, 1320, 799);
g.DrawString(property.RetirementPlan.ToString(), font, brushColor, 1463, 795);
//g.DrawString("X", font, brushColor, 1620, 799);
//right col
g.DrawString(property.FederalTaxWithheld, font, brushColor, 2275, 190, rightAlign);
g.DrawString(property.SocialSecurityTaxWithheld, font, brushColor, 2275, 290, rightAlign);
g.DrawString(property.MedicareTaxWithheld, font, brushColor, 2275, 390, rightAlign);
//g.DrawString(property.AllocatedTips, font, brushColor, 2275, 514, rightAlign);
//g.DrawString(property.DependentCareBenefits, font, brushColor, 2275, 626, rightAlign);
g.DrawString(property.TwelveARight, font, brushColor, 2275, 690, rightAlign);
g.DrawString(property.TwelveALeft, font, brushColor, 1900, 690, rightAlign);
//g.DrawString(property.TwelveBRight, font, brushColor, 2275, 850, rightAlign);
//g.DrawString(property.TwelveBLeft, font, brushColor, 1900, 850, rightAlign);
//g.DrawString(property.TwelveCRight, font, brushColor, 2275, 962, rightAlign);
//g.DrawString(property.TwelveCLeft, font, brushColor, 1900, 962, rightAlign);
//g.DrawString(property.TwelveDRight, font, brushColor, 2275, 1074, rightAlign);
//g.DrawString(property.TwelveDLeft, font, brushColor, 1900, 1074, rightAlign);
//bottom row
g.DrawString(employerInfo.State, font, brushColor, 30, 1142);
g.DrawString(employerInfo.SIN, font, brushColor, 450, 1142, centerAlign);
g.DrawString(property.StateWages, font, brushColor, 1050, 1142, rightAlign);
g.DrawString(property.StateIncomeTax, font, brushColor, 1380, 1142, rightAlign);
//g.DrawString(property.LocalWages, font, brushColor, 1824, 1142);
//g.DrawString(property.LocalIncomeTax, font, brushColor, 2284, 1142);
//g.DrawString(property.LocalityName, font, brushColor, 2704, 1142);
g.Flush();
if (fourPerPage == true)
{//four images on one page
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);
g.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
else
{//two images per page
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(bmp, ImageFormat.Bmp);
pic.ScalePercent(80);
document.Add(pic);
g.Dispose();
}
}
document.Add(imageTable);
//document.NewPage();
document.Close();
//attempt to add a bmp
//document.Add(instructions);
//attempt to add pdfsharp to it
//PdfSharp.Pdf.PdfDocument uno = PdfSharp.Pdf.IO.PdfReader.Open(@"\\ProdFS\DocumentArchive$\MessagingService\W2\W2_Images_For_Creation\" + employerInfo.taxYear + "_Form_W2_Instructions.pdf", PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);
//document.Add(uno);
var InstructionStream = new MemoryStream(System.IO.File.ReadAllBytes(@"\\ProdFS\DocumentArchive$\MessagingService\W2\W2_Images_For_Creation\" + employerInfo.taxYear + "_Form_W2_Instructions.pdf"));
InstructionStream.Position = 0;
InstructionStream.CopyTo(tempImageStream);
using (FileStream fileStream = File.OpenRead(@"\\ProdFS\DocumentArchive$\MessagingService\W2\W2_Images_For_Creation\" + employerInfo.taxYear + "_Form_W2_Instructions.pdf"))
{
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
memStream.CopyTo(tempImageStream);
fileStream.CopyTo(tempImageStream);
//tempImageStream.CopyTo(memStream);
//tempImageStream.CopyTo(fileStream);
memStream.CopyToAsync(tempImageStream);
fileStream.CopyToAsync(tempImageStream);
//tempImageStream.CopyToAsync(memStream);
//tempImageStream.CopyToAsync(fileStream);
}
W2ImageDictionary.Add(property.EmployeeID, tempImageStream);
}
return W2ImageDictionary;
}
将流保存为PDF:
public void Save(Dictionary<string, MemoryStream> dictionaryOfImages, int year, string path)
{
string saveToFolder = path;
if (!System.IO.Directory.Exists(saveToFolder))
{
System.IO.Directory.CreateDirectory(saveToFolder);
}
foreach (var key in dictionaryOfImages.Keys)
{
var fileStream = File.Create(saveToFolder + "/" + key + "_" + year + ".pdf");
dictionaryOfImages[key].Seek(0, SeekOrigin.Begin);
dictionaryOfImages[key].CopyTo(fileStream);
fileStream.Close();
}
}