In this short article I will explain how to export ASP.Net Repeater Control to PDF i.e. Portable Document Format in ASP.Net using iTextSharp PDF Conversion Library.
HTML Markup
Below is the HTML Markup of the page. It contains an ASP.Net Repeater control inside ASP.Net Panel and an Export Button.
<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="Repeater1" 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>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
Namespaces
You will have to inherit the following Namespaces
C#
using System.IO;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
VB.Net
Imports System.IO
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Binding and populating the ASP.Net Repeater control
I am populating the Repeater control in the page load event of the ASP.Net Page
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!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");
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
If Not 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")
Repeater1.DataSource = dt
Repeater1.DataBind()
End If
End If
End Sub
Exporting the ASP.Net Repeater Control to PDF
Below is the code which is executed on the click event handler of the Export Button
C#
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
VB.Net
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Panel1.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
End Sub
Screenshots
Demo
Downloads
You can download the complete source code in C# and VB.Net using the download link provided below