In this article I will explain with an example, how to export Repeater Control to PDF Document in ASP.Net using iTextSharp.
 
 

Download iTextSharp and XmlWorkerHelper Libraries

In order to install iTextSharp and XmlWorkerHelper library from Nuget, please refer the following articles.
 
 

HTML Markup

The following HTML Markup consists of:
Panel – For displaying content.
Repeater – For displaying data.
The Repeater consists of following Templates.
ItemTemplate - The content of this template will be repeated for each record present in its DataSource.
ItemTemplate consists of three Label controls.
Label - For displaying the columns value.
Button – For exporting the GridView to PDF file.
The Button has been assigned with a OnClick event handler.
<asp:Panel ID="Panel1" runat="server">
    <table border="1">
        <tr>
            <td align="center" style="background-color:#6C6C6C;line-height:200%;color:White;width:100px;font-size:10pt;font-family:Arial">Name
            </td>
            <td align="center" style="background-color:#6C6C6C;line-height:200%;color:White;width:100px;font-size:10pt;font-family:Arial">City
            </td>
            <td align="center" style="background-color:#6C6C6C;line-height:200%;color:White;width:100px;font-size:10pt;font-family:Arial">Country
            </td>
        </tr>
        <asp:Repeater ID="rptCustomers" runat="server">
            <ItemTemplate>
                <tr>
                    <td align="center" style="background-color:#eee;color:black;font-size:10pt;width:100px;font-family:Arial">
                        <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
                    </td>
                    <td align="center" style="background-color:#eee;color:black;font-size:10pt;width:100px;font-family:Arial">
                        <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City")%>'></asp:Label>
                    </td>
                    <td align="center" style="background-color:#eee;color:black;font-size:10pt;width:100px;font-family:Arial">
                        <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country")%>'></asp:Label>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
</asp:Panel>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
 
VB.Net
Imports System.IO
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
 
 

Binding and populating the ASP.Net Repeater control

Inside the Page_Load event handler, the Repeater is populated with dummy data using DataTable.
Note: For more details on how to create DataTable dynamically and bind to GridView, please refer my article Dynamically create DataTable and bind to GridView in ASP.Net.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] {
            new DataColumn("Name"),
            new DataColumn("City"),
            new DataColumn("Country")
        });
        dt.Rows.Add("Mudassar", "Mumbai", "India");
        dt.Rows.Add("John", "Chicago", "USA");
        dt.Rows.Add("Rick", "London", "UK");
        dt.Rows.Add("Mike", "California", "USA");
        dt.Rows.Add("Peter", "Toronto", "Canada");
        rptCustomers.DataSource = dt;
        rptCustomers.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {
            New DataColumn("Name"),
            New DataColumn("City"),
            New DataColumn("Country")
        })
        dt.Rows.Add("Mudassar", "Mumbai", "India")
        dt.Rows.Add("John", "Chicago", "USA")
        dt.Rows.Add("Rick", "London", "UK")
        dt.Rows.Add("Mike", "California", "USA")
        dt.Rows.Add("Peter", "Toronto", "Canada")
        rptCustomers.DataSource = dt
        rptCustomers.DataBind()
    End If
End Sub
 
 

Exporting the ASP.Net Repeater Control to PDF

When Export button is clicked, the PDF is exported.
The StringWriter and HtmlTextWriter class objects are created and the Repeater is exported to an HTML string using StringReader class.
Then, the Repeater is rendered into an HTML string using RenderControl which accepts HtmlTextWriter class object as parameter.
After that, the generated HTML is added to the iTextSharp PDF document using Document class.
Finally, PDF document is opened and the Repeater data is written using ParseXHtml method of XmlWorkerHelper class which converts it to PDF document and saves it to the MemoryStream class object.
C#
protected void btnExport_Click(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            Panel1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                pdfDoc.Close();
 
                Response.ContentType "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
        }
    }
}
 
VB.Net
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
    Using sw As New StringWriter()
        Using hw As New HtmlTextWriter(sw)
            Panel1.RenderControl(hw)
            Using sr As New StringReader(sw.ToString())
                Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0F)
                Using memoryStream As New MemoryStream()
                    Dim writer As PdfWriter PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
                    pdfDoc.Open()
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
                    pdfDoc.Close()
 
                    Response.ContentType "application/pdf"
                    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf")
                    Response.Cache.SetCacheability(HttpCacheability.NoCache)
                    Response.Write(pdfDoc)
                    Response.End()
                End Using
            End Using
        End Using
    End Using
End Sub
 
 

Screenshots

The Form

Export Repeater Control to PDF Document in ASP.Net using iTextSharp
 

Exported PDF

Export Repeater Control to PDF Document in ASP.Net using iTextSharp
 
 

Demo

 
 

Downloads