2

I have this code below please help me to repeat the table headers in each page. I have tried with few options availavle on net but they are not working. I am using ItextSharp for the first time. Thanks

    int columns = gv.Columns.Count;
    int rows = gv.Rows.Count;
    int tableRows = rows + 3;   
    string flName = "";

    if (gv.ID == "gvShares")
    {
        flName = "Share_Statement.pdf";
    }

    if (gv.ID == "gvSavingAccStmt")
    {
        flName = "Main_Saving_Account_Statement.pdf";
    }

    Document Doc = new Document();
    PdfWriter.GetInstance(Doc, Response.OutputStream);
    Doc.Open();

    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
    Font times = new Font(bfTimes, 9, Font.NORMAL, Color.BLACK);

    //string imageFilePath = HttpContext.Current.Server.MapPath("/Image/Sacco_Logo.jpg");

    string test = System.Web.HttpContext.Current.Server.MapPath(".");

    string substr = test.Remove(test.Length - 6);

    //string imageFilePath = "\\app\\Image\\Sacco_Logo.jpg";//"\\app\\Image\\Sacco_Logo.jpg";
    iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(substr + "\\Image\\Sacco_Logo.jpg");

    //Resize image depend upon your need
    jpg.ScaleToFit(75f, 75f);

    jpg.SpacingAfter = 10f;

    jpg.Alignment = Element.ALIGN_LEFT;

    //Doc.Add(jpg);

    PdfPTable table = new PdfPTable(1);
    table.DefaultCell.Border = PdfPCell.NO_BORDER;
    table.WidthPercentage = 100;

    PdfPCell cell = new PdfPCell( table.DefaultCell);
    cell.Border = PdfPCell.BOTTOM_BORDER;
    cell.PaddingBottom = 5;


    Phrase phrase = new Phrase();
    phrase.Add(new Chunk(jpg, 0, 0));
    phrase.Add(new Chunk("Kenya Diaspora Sacco"));        
    cell.AddElement( phrase );
    table.AddCell(cell);

    Doc.Add(table);

    Phrase phrNewLine = new Phrase("\n");

    Doc.Add(phrNewLine);

    Paragraph TitlePhrasePh = new Paragraph("Membership Savings Account Statement", times);
    TitlePhrasePh.SetAlignment("Center");        
    Doc.Add(TitlePhrasePh);

    Doc.Add(phrNewLine);

    Paragraph PhUsername = new Paragraph("Name: " + Username, times);
    PhUsername.SetAlignment("Left");
    Doc.Add(PhUsername);

    Paragraph PhAccount = new Paragraph("Account Number: " + AccountNumber, times);
    PhAccount.SetAlignment("Left");
    Doc.Add(PhAccount);


    iTextSharp.text.Table gridViewTable = new iTextSharp.text.Table(columns, tableRows);
    gridViewTable.BorderWidth = 1;
    gridViewTable.BorderColor = new Color(0, 0, 255);
    gridViewTable.Cellpadding = 1;
    gridViewTable.Cellspacing = 1;

    gridViewTable.AddCell(new Paragraph("Date", times));
    gridViewTable.AddCell(new Paragraph("Type", times));
    gridViewTable.AddCell(new Paragraph("Description", times));

    if (gv.ID == "gvShares")
    {
        gridViewTable.AddCell(new Paragraph("Sell[KHSH]", times));
    }

    gridViewTable.AddCell(new Paragraph("Debit[KHSH]", times));

    if (gv.ID == "gvShares")
    {
        gridViewTable.AddCell(new Paragraph("Buy[KHSH]", times));
    }

    gridViewTable.AddCell(new Paragraph("Credit[KHSH]", times));

    if (gv.ID == "gvShares")
    {
        gridViewTable.AddCell(new Paragraph("Balance", times));
    }
    else if (gv.ID == "gvSavingAccStmt")
    {
        gridViewTable.AddCell(new Paragraph("Balance[KHSH]", times));
    }

    for (int rowCounter = 0; rowCounter < rows; rowCounter++)
    {
        for (int columnCounter = 0; columnCounter < columns; columnCounter++)
        {
            string strValue = gv.Rows[rowCounter].Cells[columnCounter].Text;
            gridViewTable.AddCell(new Paragraph(strValue, times));
        }
    }



    Doc.Add(gridViewTable);
    Doc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment; filename=" + flName);
    Response.End();
Shantanu Sen
  • 221
  • 3
  • 4
  • 18
  • possible duplicate of [How repeat headers of pdfpTable on each page](http://stackoverflow.com/questions/2062983/how-repeat-headers-of-pdfptable-on-each-page) – Joel Anair Jan 15 '14 at 16:51

2 Answers2

16

You need to use the HeaderRows property of the PdfPTable object.

PdfPTable table = new PdfPTable(1);
table.HeaderRows = 1;

After setting the HeaderRows property to a value n, the next n rows you add to your table will be considered header rows, and will be repeated on each new page.

TimS
  • 2,085
  • 20
  • 31
  • I was trying to put the same .. but giving me weird results can you point out where i can include code ... by looking at my code above – Shantanu Sen Feb 12 '13 at 14:54
  • Which particular cell, or cells, do you want to be the table header? – TimS Feb 12 '13 at 21:56
  • If you see in the code its the explicitly defined column name forms the first row... – Shantanu Sen Feb 13 '13 at 11:50
  • You should not be using a Table, but rather a PdfPTable, and setting the HeaderRows property after creating the table. I would also suggest removing unnecessary code from your example. – TimS Feb 14 '13 at 06:01
2

Like this:

        PdfPTable table = new PdfPTable(12);
        table.HeaderRows = 1; /*---->> this property repeats the headers of an iTextSharp PdfPTable on each page */
        table.TotalWidth = 900f;
        table.LockedWidth = true;
        //table.HorizontalAlignment = 0;
        float[] widths = new float[] { 20f, 32f, 13f, 24f, 20f, 20f, 30f, 15f, 15f, 20f, 20f, 60f };
        table.SetWidths(widths);
Mr. Munoz
  • 69
  • 1
  • 3
  • 13