0

I am binding the Datatable data in PDF table and there are 100+ rows so the data is consisted of many pages so want to repeat the header of the table on each pages.How can i do this?

void ExportDataTableToPdf(DataTable dtblTable, String strPdfPath, string strHeader)
{
    System.IO.FileStream fs = new FileStream(strPdfPath, FileMode.Create, FileAccess.Write, FileShare.None);
    Document document = new Document();
    document.SetPageSize(iTextSharp.text.PageSize.A4);
    PdfWriter writer = PdfWriter.GetInstance(document, fs);
    document.Open();

    //Report Header
    BaseFont bfntHead = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font fntHead = new Font(bfntHead, 16, 1, Color.GRAY);
    Paragraph prgHeading = new Paragraph();
    prgHeading.Alignment = Element.ALIGN_CENTER;
    prgHeading.Add(new Chunk(strHeader.ToUpper(), fntHead));
    document.Add(prgHeading);

    //Author
    Paragraph prgAuthor = new Paragraph();
    BaseFont btnAuthor = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font fntAuthor = new Font(btnAuthor, 8, 2, Color.GRAY);
    prgAuthor.Alignment = Element.ALIGN_RIGHT;
    // prgAuthor.Add(new Chunk("Author : Dotnet Mob", fntAuthor));
    prgAuthor.Add(new Chunk("\nPrint Date : " + DateTime.Now.ToShortDateString(), fntAuthor));
    document.Add(prgAuthor);

    //Add a line seperation
    Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, Color.BLACK, Element.ALIGN_LEFT, 1)));
    document.Add(p);

    //Add line break
    document.Add(new Chunk("\n", fntHead));

    //Write the table
    PdfPTable table = new PdfPTable(dtblTable.Columns.Count);
    //Table header
    BaseFont btnColumnHeader = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font fntColumnHeader = new Font(btnColumnHeader, 10, 1, Color.WHITE);
    for (int i = 0; i < dtblTable.Columns.Count; i++)
    {
        PdfPCell cell = new PdfPCell();
        cell.BackgroundColor = Color.GRAY;
        cell.AddElement(new Chunk(dtblTable.Columns[i].ColumnName.ToUpper(), fntColumnHeader));
        table.AddCell(cell);
    }
    //table Data
    for (int i = 0; i < dtblTable.Rows.Count; i++)
    {
        for (int j = 0; j < dtblTable.Columns.Count; j++)
        {
            table.AddCell(dtblTable.Rows[i][j].ToString());
        }
    }

    document.Add(table);
    document.Close();
    writer.Close();
    fs.Close();
}
CSDev
  • 3,177
  • 6
  • 19
  • 37

1 Answers1

0

I can't find the documentation, but I think you should add a OnEndPage event, and in this event, you can start a new table with headers.

I think if you want to repeat headers on each page, you need to create a separated table for each page.

baruchiro
  • 5,088
  • 5
  • 44
  • 66
  • While it certainly is possible to create table headers using page events, the solution hinted at by Oleg Bondarenko in his comments to the question is by far easier and better looking. – mkl Jun 24 '19 at 20:13
  • @mkl I'm not sure his solution works – baruchiro Jun 25 '19 at 04:04
  • 1
    Rest assured, it does. See all the questions this one has been closed as a duplicate of. – mkl Jun 25 '19 at 04:37