Reputation:
I am creating a memory stream with a pdf in it. The pdf is supposed to have a table of 4 different images that are written on. Instead the pdf has one table filled out completely with the first image, and a second page with the correct 4 images. It's supposed to look kind of like this Correct output
but instead this image is on top of it incorrect output
The error happens when fourPerPage is passed in true. When passed false it works correctly and puts the 4 images on two pages without a table.
here is code:
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;
}
Upvotes: 1
Views: 84
Reputation: 95908
You call
document.Add(imageTable);
inside the for
loop. Thus, you add the table to the document four times!
The first time there is only one cell, i.e. no complete row, nothing is drawn.
The second time there are two cells, i.e. one complete row, so one row is drawn.
Copy 2 | Copy 2
The third time there are three cells, i.e. still only one complete row, so the first row is drawn again.
Copy 2 | Copy 2
Copy 2 | Copy 2
The fourth time there are four cells, i.e. two complete rows, now the whole table is drawn.
Copy 2 | Copy 2
Copy 2 | Copy 2
Copy 2 | Copy 2
Copy B | Copy C
Try calling that line only in the last iteration, i.e.
if (i == 4)
document.Add(imageTable);
Now the table should only be drawn once, only when it's finished.
Upvotes: 1