I am trying to generate four copies of the pdf report by following the given link:
i wrote the following code it is showing me out in improper format
private void tbEdit_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "PDF Documents (*.pdf)|*.pdf";
sfd.FileName = "FamilywiseVouchers-" + cmbPYear.Text + "-" + cmbMonth.Text; //+ "-" + DateTime.Now.ToShortDateString();
if (sfd.ShowDialog() == DialogResult.OK)
{
Document doc = new Document(PageSize.LEGAL.Rotate());
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
// PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
PdfPTable tableM = new PdfPTable(10);
tableM.TotalWidth = 1000f;
tableM.LockedWidth = true;
PdfPCell cell = new PdfPCell(new Phrase("This is table 1"));
cell.Colspan = 10;
cell.HorizontalAlignment = 1;
tableM.AddCell(cell);
var grouped = from x in sTable.AsEnumerable()
group x by new { a = x["ParentID"] } into g
select new
{
Value = g.Key,
ColumnValues = g
};
DataTable dtfinal = null;
foreach (var key in grouped)
{
dtfinal = sTable.Clone();
foreach (var columnValue in key.ColumnValues)
{
dtfinal.ImportRow(columnValue);
}
con = new SqlDbConnect();
con.SqlQuery("select logo,SystemName,Address,Phone,Email FROM tblSystem");
con.RdrEx();
while (con.Rdr.Read())
{
PdfPTable table1 = new PdfPTable(2);
table1.DefaultCell.Padding = 10f;
table1.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
table1.DefaultCell.Border = 0;
table1.HorizontalAlignment = Element.ALIGN_CENTER;
table1.TotalWidth = 500f;
table1.LockedWidth = true;
float[] widths1 = new float[] { 0.7f, 3f };
table1.SetWidths(widths1);
Byte[] bytes = (Byte[])con.Rdr[0];
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(bytes);
image.ScaleAbsolute(90f, 70f);
PdfPCell bottom1 = new PdfPCell((image));
bottom1.Padding = 5f;
bottom1.BackgroundColor = iTextSharp.text.Color.WHITE;
bottom1.Border = 0;
bottom1.HorizontalAlignment = 1;
table1.AddCell(bottom1);
PdfPTable nested1 = new PdfPTable(1);
float[] width = new float[] { 0.1f };
nested1.SetWidths(width);
nested1.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
nested1.DefaultCell.Padding = 7f;
nested1.DefaultCell.HorizontalAlignment = 1;
nested1.DefaultCell.Border = 0;
nested1.AddCell(new Phrase(con.Rdr[1].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15)));
nested1.AddCell(new Phrase(con.Rdr[2].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14)));
nested1.AddCell(new Phrase("Email: " + con.Rdr[4].ToString() + "\t Phone No: " + con.Rdr[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)));
// nested1.AddCell(new Phrase("Phone No: " + con.Rdr[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14)));
PdfPCell nesthousing1 = new PdfPCell(nested1);
nesthousing1.Colspan = 2;
nesthousing1.Padding = 0f;
nesthousing1.Border = 0;
table1.AddCell(nesthousing1);
doc.Add(table1);
Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(1.0F, 100.0F, iTextSharp.text.Color.BLACK, Element.ALIGN_LEFT, 1)));
doc.Add(p);
}
con.conClose();
PdfPTable tableb = new PdfPTable(4);
float[] widthim = new float[] { 0.1f, 0.1f, 0.1f, 0.05f };
tableb.SetWidths(widthim);
tableb.DefaultCell.Padding = 10f;
tableb.HorizontalAlignment = Element.ALIGN_CENTER;
tableb.TotalWidth = 550f;
tableb.LockedWidth = true;
tableb.SpacingBefore = 10f;
tableb.SpacingAfter = 10f;
PdfPCell header = new PdfPCell(new Phrase("Parents Details", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)));
header.Indent = 10;
header.HorizontalAlignment = 1;
header.Padding = 10f;
header.Colspan = 4;
tableb.AddCell(header);
PdfPTable nested = new PdfPTable(3);
float[] widthi = new float[] { 0.1f, 0.1f, 0.1f };
nested.SetWidths(widthi);
nested.DefaultCell.Padding = 10f;
nested.AddCell(new Phrase("Fees Month: " + cmbMonth.Text, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Issue Date: " + dtfinal.Rows[0]["IssueDate"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Due Date: " + dtfinal.Rows[0]["DueDate"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Family Code: " + dtfinal.Rows[0]["ParentID"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Father Name: " + dtfinal.Rows[0]["FName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Phone No. " + dtfinal.Rows[0]["FPhone"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
PdfPCell nesthousing = new PdfPCell(nested);
nesthousing.Colspan = 4;
nesthousing.Padding = 0f;
tableb.AddCell(nesthousing);
doc.Add(tableb);
PdfPTable table = new PdfPTable(10);
table.TotalWidth = 575f;
table.LockedWidth = true;
float[] widths = new float[] { 0.03f, 0.05f, 0.08f, 0.05f, 0.05f, 0.04f, 0.04f, 0.05f, 0.05f, 0.05f };
table.SetWidths(widths);
table.DefaultCell.Padding = 10f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.SpacingBefore = 5f;
PdfPCell headerb = new PdfPCell(new Phrase("Fee Voucher", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)));
headerb.Indent = 10;
headerb.HorizontalAlignment = 1;
headerb.Padding = 10f;
headerb.Colspan = 10;
table.AddCell(headerb);
table.AddCell(new Phrase("S.I", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Reg No", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Name", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Class", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Section", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Fee", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Other", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Payable", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Arrears", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("Net Bal", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
for (int i = 0; i < dtfinal.Rows.Count; i++)
{
table.AddCell(new Phrase("" + (i + 1), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
for (int j = 8; j < dtfinal.Columns.Count - 0; j++)
{
table.AddCell(new Phrase("" + dtfinal.Rows[i][j], FontFactory.GetFont(FontFactory.HELVETICA, 10)));
}
}
int Fee = Convert.ToInt32(dtfinal.Compute("sum(Fees)", ""));
int Other = Convert.ToInt32(dtfinal.Compute("sum(Others)", ""));
int Reci = Convert.ToInt32(dtfinal.Compute("sum(Recievable)", ""));
//int Arr = Convert.ToInt32(dtfinal.Compute("sum(Arrears)", ""));
int Net = Convert.ToInt32(dtfinal.Compute("sum(NetBal)", ""));
table.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
table.AddCell(new Phrase("" + Fee, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11)));
table.AddCell(new Phrase("" + Other, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11)));
table.AddCell(new Phrase("" + Reci, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11)));
table.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11)));
table.AddCell(new Phrase("" + Net, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11)));
doc.Add(table);
tableM.WriteSelectedRows(0, -1, doc.Left, doc.Top, writer.DirectContent);
tableM = new PdfPTable(10);
tableM.TotalWidth = 1000f;
tableM.LockedWidth = true;
cell = new PdfPCell(new Phrase("This is table 1 Copy"));
cell.Colspan = 10;
cell.HorizontalAlignment = 1;
tableM.AddCell(cell);
doc.Add(tableM);
doc.NewPage();
}
doc.Close();
MessageBox.Show("Familywise Voucher Generated Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}