I am trying to create a pdf in the attached format by using grouping using c#. The first page is fine but next page still first-page first column name exists. while creating a pdf new page get the first page data
I used
DeleteBodyRows()
for removing table rows but still the same :
foreach (DataRow row in dataTable.Rows)
{
//write in new row
var datatable = dataTable;
var departments = from r in datatable.Rows.OfType<DataRow>()
group r by r["emp_reader_id"] into g
select new { emp_reader_id = g.Key, Data = g };
foreach (var department in departments)
{
foreach (var roww in department.Data)
{
iii++;
if (ii == 0)
{
PdfPCell cell1333 = new PdfPCell(new iTextSharp.text.Phrase("Name " + ":" + roww.ItemArray[1].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell1333.Colspan = 8;
cell1333.Border = 0;
cell1333.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
table.AddCell(cell1333);
PdfPCell cell1 = new PdfPCell(new iTextSharp.text.Phrase("E.Code " + ":" + roww.ItemArray[2].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell1.Colspan = 8;
cell1.Border = 0;
cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
table.AddCell(cell1);
PdfPCell cell144 = new PdfPCell(new iTextSharp.text.Phrase("Department " + ":" + roww.ItemArray[4].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell144.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
cell144.Colspan = 8;
cell144.Border = 0;
cell144.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(cell144);
PdfPCell cell155 = new PdfPCell(new iTextSharp.text.Phrase("Designation " + ":" + roww.ItemArray[5].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell155.Colspan = 8;
cell155.Border = 0;
cell155.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
cell155.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(cell155);
PdfPCell cell166 = new PdfPCell(new iTextSharp.text.Phrase("Name Of Facility " + ":" + roww.ItemArray[6].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
cell166.Colspan = 8;
cell166.Border = 0;
cell166.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
cell166.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.AddCell(cell166);
DataTable dtp1 = new DataTable();
dtp1.Columns.Add("Date", typeof(string));
dtp1.Columns.Add("F1", typeof(string));
dtp1.Columns.Add("F2", typeof(string));
dtp1.Columns.Add("F3", typeof(string));
dtp1.Columns.Add("F4", typeof(string));
dtp1.Columns.Add("Hours", typeof(string));
dtp1.Columns.Add("Remarks", typeof(string));
for (int i = 0; i < 7; i++)
{
PdfPCell cell = new PdfPCell(new iTextSharp.text.Phrase(dtp.Columns[i].ColumnName, new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.ITALIC, iTextSharp.text.Color.WHITE)));
cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#2980b9"));
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
table.AddCell(cell);
}
}
ii++;
DateTime date = DateTime.Parse(roww.ItemArray[3].ToString(), System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
string strDate = String.Format("{0:dd/MM/yyyy}", date);
PdfPCell cell19 = new PdfPCell(new iTextSharp.text.Phrase(strDate, new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK)));
cell19.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
table.AddCell(cell19);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
table.AddCell(string.Empty);
}
ii = 0;
pdfDoc.Add(table);
table.DeleteBodyRows();
pdfDoc.NewPage();
}
break;
}
All new page contains the first page's first row continuously. Above is the code I tried. Any help ? Thanks!
add below line next to table.DeleteBodyRows()
To remove first row
table.DeleteRow(0);