In this article I will explain with an example, how to export GridView to PDF with Header Repeated on each Page using iTextSharp in ASP.Net with C# and VB.Net.
When the Export Button is clicked, a loop will be executed over the GridView Pages using PageCount property and GridView records for specific Page will be added to a new page of PDF.
Thus, using this technique each Page of PDF will have records of one Page of GridView with Header.
 
 
Database
For this article I am making use of the Microsoft’s Northwind Database. Download and install instructions are provided in the link below.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView and a Button. The AllowPaging property is set to True and the OnPageIndexChanging event has been specified for the GridView.
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
    runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="Contact Name" ItemStyle-Width="150px" />
        <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick = "ExportToPDF" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
 
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
 
 
Binding the GridView
Inside the Page Load event, the GridView is populated with records from the Customers table of the Northwind database.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub
 
Private Sub BindGrid()
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(strConnString)
        Using cmd As New SqlCommand("SELECT * FROM Customers")
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub
 
 
Implement Paging in GridView
Inside the OnPageIndexChanging event, the PageIndex property of the GridView is set with the NewPageIndex property value and then the BindGrid function is called.
C#
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}
 
VB.Net
Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
    GridView1.PageIndex = e.NewPageIndex
    Me.BindGrid()
End Sub
 
 
Export GridView with Paging Enabled to PDF
When the Export Button is clicked, first the Page Size of the iTextSharp PDF document is set and then a loop is executed over the GridView Pages using its PageCount property.
Inside the loop, GridView is populated for each Page, rendered into HTML string and added to iTextSharp PDF document.
Then a new Page is added to the iTextSharp PDF document and this continues for all Pages of the GridView.
Finally, the iTextSharp PDF document is sent for download using Response.OutputStream.
Note: The iTextSharp DLL supplied with the code sample of this article has been modified to allow GridView Styles, Colors and Formatting. Hence, if you use any other copy of iTextSharp DLL the GridView Styles, Colors and Formatting won’t be rendered in the exported PDF.
C#
protected void ExportToPDF(object sender, EventArgs e)
{
    //Set the Size of PDF document.
    Rectangle rect = new Rectangle(500, 300);
    Document pdfDoc = new Document(rect, 10f, 10f, 10f, 0f);
 
    //Initialize the PDF document object.
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
 
    //Loop through GridView Pages.
    for (int i = 0; i < GridView1.PageCount; i++)
    {
        //Set the Page Index.
        GridView1.PageIndex = i;
 
        //Hide Page as not needed in PDF.
        GridView1.PagerSettings.Visible = false;
 
        //Populate the GridView with records for the Page Index.
        this.BindGrid();
 
        //Render the GridView as HTML and add to PDF.
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                GridView1.RenderControl(hw);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                StringReader sr = new StringReader(sw.ToString());
                htmlparser.Parse(sr);
            }
        }
 
        //Add a new Page to PDF document.
        pdfDoc.NewPage();
    }
    //Close the PDF document.
    pdfDoc.Close();
 
    //Download the PDF file.
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
}
 
VB.Net
Protected Sub ExportToPDF(ByVal sender As Object, ByVal e As EventArgs)
    'Set the Size of PDF document.
    Dim rect As Rectangle = New Rectangle(500, 300)
    Dim pdfDoc As Document = New Document(rect, 10.0F, 10.0F, 10.0F, 0.0F)
 
    'Initialize the PDF document object.
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
 
    'Loop through GridView Pages.
    Dim i As Integer = 0
    Do While (i < GridView1.PageCount)
 
        'Set the Page Index.
        GridView1.PageIndex = i
 
        'Hide Page as not needed in PDF.
        GridView1.PagerSettings.Visible = False
 
        'Populate the GridView with records for the Page Index.
        Me.BindGrid()
 
        'Render the GridView as HTML and add to PDF.
        Using sw As New StringWriter()
            Using hw As New HtmlTextWriter(sw)
                GridView1.RenderControl(hw)
                Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
                Dim sr As StringReader = New StringReader(sw.ToString)
                htmlparser.Parse(sr)
            End Using
        End Using
 
        'Add a new Page to PDF document.
        pdfDoc.NewPage()
        i = (i + 1)
    Loop
 
    'Close the PDF document.
    pdfDoc.Close()
 
    'Download the PDF file.
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.Write(pdfDoc)
    Response.End()
End Sub
 
 
Screenshots
GridView with Paging enabled
Export GridView to PDF with Header Repeated on each Page in ASP.Net
 
PDF with GridView Headers repeated on each Page
Export GridView to PDF with Header Repeated on each Page in ASP.Net
 
 
Demo
 
 
Downloads