The goal of this project is to create a dll that will take in employee and employer information then return W2(s) in pdf form. Then the main program will cycle through them and email/print. Currently, the program is adding the SSN and the edited pdf to a dictionary and returning it. When I try to access the pdf from the dictionary to save or email it won't let me. I have a few questions:
- Am I headed in the right direction?
- How can I save the pdf since it doesn't have a .save similar to images?
-
Should I attempt serializing instead of dictionary? - I have 0 experience with this though.
private void createButton_Click(object sender, RoutedEventArgs e) { var employerInfo = new Employer { City = "Hometown", AddressName = "101 Dalmation ln", State = "MO", Name = "WhileyOnes", ZipCode = "36963", EIN = "1234", SIN = "4321", ControlNumber = "12345" }; var listOfEmployeeInfo = new List<Employee>() { new Employee(){ GrossWages = 3333.02, SocialSecurityWages = 22.22, MedicareWages = 11.23, FederalTaxWithheld = 633.02, SocialSecurityTaxWithheld = 100.01, MedicalTaxWithheld = 50.00, SocialSecurityTips = 36.99, NonqualifiedPlans = "NONQUALIFIED", AllocatedTips = 50.22, DependentCareBenefits = "", TwelveALeft = "D", TwelveARight = 333.33, TwelveBLeft = "", TwelveBRight = 222.22, TwelveCLeft = "", TwelveCRight = 11.11, TwelveDLeft = "F", TwelveDRight = 123.21, EmployeeSSN = "123456788", StatutoryEmployee = true, RetirementPlan = false, ThirdPartySickPay = false, Other = "23456.22", EmployeeName = "Carl B Flinn", StateWages = 200.02, StateIncomeTax = 350.00, LocalWages = 200.00, LocalIncomeTax = 50.33, LocalityName = "LOC NAME", AddressName = "200 Oak Meadows", City = "Yankee", State = "AK", ZipCode = "63936" }, new Employee(){ GrossWages = 2424.22, SocialSecurityWages = 2332.22, MedicareWages = 11.23, FederalTaxWithheld = 993.00, SocialSecurityTaxWithheld = 663.33, MedicalTaxWithheld = 100.21, SocialSecurityTips = 22.11, AllocatedTips = 34.10, DependentCareBenefits = "", LocalIncomeTax = 33.10, LocalWages = 34.23, LocalityName = "benson", NonqualifiedPlans = "NONQUALIFIED", TwelveALeft = "", TwelveARight = 33.22, TwelveBLeft = "P", TwelveBRight = 11.12, TwelveCLeft = "", TwelveCRight = 22.11, TwelveDLeft = "", TwelveDRight = 234.11, EmployeeSSN = "123456799", StatutoryEmployee = true, RetirementPlan = true, ThirdPartySickPay = false, Other = "2233.11", EmployeeName = "Elvis A Presley", AddressName = "777 Lucky Lane", City = "Tajmahal", State = "PN", ZipCode = "63969", StateWages = 22113.11, StateIncomeTax = 350.00 }, new Employee(){ GrossWages = 3333.02, SocialSecurityWages = 22.22, FederalTaxWithheld = 633.02, SocialSecurityTaxWithheld = 100.01, SocialSecurityTips = 1.34, StateIncomeTax = 77.77, StateWages = 22233344.22, AllocatedTips = 444.44, LocalIncomeTax = 23.87, MedicareWages = 10000.33, MedicalTaxWithheld = 50.22, NonqualifiedPlans = "NONQUALIFIED", DependentCareBenefits = "", TwelveALeft = "D", TwelveARight = 333.33, TwelveBLeft = "", TwelveBRight = 22.11, TwelveCLeft = "DD", TwelveCRight = 789.44, TwelveDLeft = "", TwelveDRight = 456.65, EmployeeSSN = "123456778", StatutoryEmployee = true, RetirementPlan = false, ThirdPartySickPay = false, Other = "23456.22", EmployeeName = "Spongebob Squarepants", AddressName = "124 Conch Street", City = "Bikini Bottom", State = "CO", ZipCode = "96970", LocalWages = 2.22, LocalityName = "Marshall" } }; var imageDictionary = new W2Generator().SendW2Images(listOfEmployeeInfo, employerInfo); using (Stream stream = File.Open(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "W-2 PDF") + "/newwtwo.pdf", FileMode.Create, FileAccess.Write)) { var binaryFormatter = new BinaryFormatter(); //breaks here binaryFormatter.Serialize(stream, imageDictionary["123456799"]); } //using (FileStream file = new FileStream(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "W-2 PDF"), FileMode.Create, FileAccess.Write)) //{ // imageDictionary.GetObjectData; //} } //----------------------------------------------------------------------------------------- public Dictionary<string, Document> SendW2Images(List<Employee> info, Employer employerInfo) { MemoryStream tempImageStream = new MemoryStream(); var W2ImageDictionary = new Dictionary<string, Document>(); var reader = new PdfReader("W2.pdf"); //loop to fill in info foreach (var property in info) { var document = new Document(reader.GetPageSize(1)); var writer = PdfWriter.GetInstance(document, tempImageStream); writer.CloseStream = false; document.Open(); var baseFont = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); var importedPage = writer.GetImportedPage(reader, 1); var contentByte = writer.DirectContent; contentByte.AddTemplate(importedPage, 0, 0); contentByte.BeginText(); contentByte.SetFontAndSize(baseFont, 12); //employee info contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.GrossWages.ToString(), 460, 568, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.SocialSecurityWages.ToString(), 460, 568, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.MedicareWages.ToString(), 320, 568, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.FederalTaxWithheld.ToString(), 320, 656, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.SocialSecurityTaxWithheld.ToString(), 320, 646, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.MedicalTaxWithheld.ToString(), 320, 636, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.SocialSecurityTips.ToString(), 367, 450, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.NonqualifiedPlans, 482, 450, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.AllocatedTips.ToString(), 562, 450, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.DependentCareBenefits, 482, 422, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.TwelveALeft, 362, 522, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.TwelveARight.ToString(), 320, 501, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.TwelveBLeft, 320, 491, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.TwelveBRight.ToString(), 320, 481, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.TwelveCLeft, 460, 712, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.TwelveCRight.ToString(), 460, 694, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.TwelveDLeft, 460, 676, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.TwelveDRight.ToString(), 562, 712, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.EmployeeSSN, 562, 694, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.StatutoryEmployee.ToString(), 562, 676, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.RetirementPlan.ToString(), 460, 568, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.ThirdPartySickPay.ToString(), 320, 568, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.Other, 320, 656, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.EmployeeName, 320, 646, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.StateWages.ToString(), 320, 636, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.StateIncomeTax.ToString(), 367, 450, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.LocalWages.ToString(), 482, 450, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.LocalIncomeTax.ToString(), 562, 450, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.LocalityName, 482, 422, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.AddressValidationValue.ToString(), 362, 522, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.AddressName, 320, 501, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.Address1, 320, 491, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, property.Address2, 320, 481, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.City, 460, 712, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.State, 460, 694, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.ZipCode, 460, 676, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.IsResidential, 562, 676, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.AddressValidationOverride, 562, 712, 0); //employer information contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.Name, 562, 712, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.EIN, 562, 694, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.SIN, 562, 676, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.ControlNumber, 562, 712, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.AddressValidationValue, 562, 694, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.AddressName, 562, 676, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.Address1, 562, 712, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.Address2, 562, 694, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.ZipCode, 562, 676, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.Country, 562, 694, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.IsResidential, 562, 676, 0); //contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.AddressValidationOverride, 562, 712, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, employerInfo.State, 562, 694, 0); contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, property.City, 562, 676, 0); contentByte.EndText(); W2ImageDictionary.Add(property.EmployeeSSN, document); document.Close(); } return W2ImageDictionary; }