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:

  1. Am I headed in the right direction?
  2. How can I save the pdf since it doesn't have a .save similar to images?
  3. 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;
        }

While I do have apps out there in C#, why must this be a DLL? That seems unnecessary at first. That is, get your code working before you worry about DLL creation.

I see 200+ lines of code but no mention of your choice of PDF creation software. The last time I did this, I cheated twice. One time I installed a PDF printer, the next time a team member used a PDF library.

I agree the dll is unnecessary, but we are using 3 tier arch and was told to create a dll for the legacy program... I'm using itextsharp to add text to the pdf I have stored.

I created a basic console app that created the pdfs flawlessly and saved them, but due to the dll aspect I'm having trouble figuring out how to access the pdf in the dictionary. The code currently is trying to access one of the 3 that were created. Once I can figure out how to save that one pdf from the dictionary the rest will come smoothly.

Here are 2 more classes if u want to test the data

namespace Entities
{
    public class Employee : IAddress
    {
        public double GrossWages { get; set; }
        public double SocialSecurityWages { get; set; }
        public double MedicareWages { get; set; }
        public double FederalTaxWithheld { get; set; }
        public double SocialSecurityTaxWithheld { get; set; }
        public double MedicalTaxWithheld { get; set; }

        public double SocialSecurityTips { get; set; }
        public string NonqualifiedPlans { get; set; }
        public double AllocatedTips { get; set; }
        public string DependentCareBenefits { get; set; }
        public string TwelveALeft { get; set; }
        public double TwelveARight { get; set; }
        public string TwelveBLeft { get; set; }
        public double TwelveBRight { get; set; }
        public string TwelveCLeft { get; set; }
        public double TwelveCRight { get; set; }
        public string TwelveDLeft { get; set; }
        public double TwelveDRight { get; set; }
        public string EmployeeSSN { get; set; } 
        public bool StatutoryEmployee { get; set; } = false;
        public bool RetirementPlan { get; set; } = false;
        public bool ThirdPartySickPay { get; set; } = false;
        public string Other { get; set; }

        public string EmployeeName { get; set; }
        public double StateWages { get; set; }
        public double StateIncomeTax { get; set; }
        public double LocalWages { get; set; }
        public double LocalIncomeTax { get; set; }
        public string LocalityName { get; set; }

        public int AddressValidationValue { get ; set ; }
        public string AddressName { get ; set ; }
        public string Address1 { get; set; }
        public string Address2 { get ; set ; }
        public string City { get ; set ; }
        public string State { get ; set; }
        public string ZipCode { get ; set ; }
        public string Country { get ; set ; }
        public bool IsResidential { get ; set ; }
        public bool AddressValidationOverride { get ; set ; }
    }
}

namespace Entities
{
    public class Employer : IAddress
    {
        public string Name { get; set; }
        public string EIN { get; set; }
        public string SIN { get; set; }
        public string ControlNumber { get; set; }
        //
        public int AddressValidationValue { get; set; }
        public string AddressName { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string ZipCode { get; set; }
        public string Country { get; set; }
        public bool IsResidential { get; set; }
        public bool AddressValidationOverride { get; set; }
        public string State { get; set; }
        public string City { get; set; }
    }
}

Thanks for the support. I ended up sending the memory stream(s) with the dictionary instead of actual documents. So I saved them for editing purposes using this...

        private void Save (Dictionary<string, MemoryStream> employees)
        {
            string saveToFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "W-2 PDF");
            foreach (var key in employees.Keys)
            {
                var fileStream = File.Create(saveToFolder + "/" + key + ".pdf");
                employees[key].Seek(0, SeekOrigin.Begin);
                employees[key].CopyTo(fileStream);
                fileStream.Close();
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.