In this article I will explain with an example, how to export Panel control to PDF document using
iTextSharp PDF library in ASP.Net using C# and VB.Net.
Download iTextSharp and XmlWorkerHelper Libraries
You can download the iTextSharp and XmlWorkerHelper libraries from the following links.
HTML Markup
The following HTML Markup consists of:
Panel – For holding the controls.
Labels – For displaying details.
Button – For exporting Panel data to PDF.
The Button has been assigned with an OnClick event handler.
<asp:Panel ID="pnlDetails" runat="server">
<table border="1" style="font-family: Arial; font-size: 10pt; width: 200px">
<tr>
<td colspan="2" style="background-color: #18B5F0; height: 18px; color: White; border: 1px solid white">
<b>Personal Details</b>
</td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><asp:Label ID="lblName" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>Age:</b></td>
<td><asp:Label ID="lblAge" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>City:</b></td>
<td><asp:Label ID="lblCity" runat="server"></asp:Label></td>
</tr>
<tr>
<td><b>Country:</b></td>
<td><asp:Label ID="lblCountry" runat="server"></asp:Label></td>
</tr>
</table>
</asp:Panel>
<br/>
<asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick="ExportToPDF" />
Namespaces
You 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
Populating Data for Export
Inside the
Page_Load event handler, the dynamic
DataTable is created and the labels are populated with dynamic
DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Populate DataTable
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] {
new DataColumn("Name"),
new DataColumn("Age") ,
new DataColumn("City"),
new DataColumn("Country") });
dt.Rows.Add("Mudassar Khan","27","Mumbai","India");
//Bind Datatable to Labels
lblName.Text = dt.Rows[0]["Name"].ToString();
lblAge.Text = dt.Rows[0]["Age"].ToString();
lblCity.Text = dt.Rows[0]["City"].ToString();
lblCountry.Text = dt.Rows[0]["Country"].ToString();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
'Populate DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(3) {
New DataColumn("Name"),
New DataColumn("Age"),
New DataColumn("City"),
New DataColumn("Country")})
dt.Rows.Add("Mudassar Khan", "27", "Mumbai", "India")
'Bind Datatable to Labels
lblName.Text = dt.Rows(0)("Name").ToString()
lblAge.Text = dt.Rows(0)("Age").ToString()
lblCity.Text = dt.Rows(0)("City").ToString()
lblCountry.Text = dt.Rows(0)("Country").ToString()
End If
End Sub
Exporting ASP.Net Panel to PDF
When the ExportToPDF button is clicked, the StringWriter and HtmlTextWriter class objects are created.
Then, the RenderControl method is called where we pass HtmlTextWriter class object as parameter.
The StringReader and Document class object is created where the PageSize is set.
Then, PdfWriter class object is created, it is used to write the information inside the PDF document.
After that, the PDF document is opened and the data is written using ParseXHtml method of XmlWorkerHelper class.
Then, the Response class properties are set.
1. ContentType – It informs the Browser about the file type. In this case it is PDF file.
2. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Finally, Document object is written to the Response which initiates the File download operation.
C#
protected void ExportToPDF(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
pnlDetails.RenderControl(hw);
using (StringReader sr = new StringReader(sw.ToString()))
{
using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f))
{
using (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 ExportToPDF(ByVal sender As Object, ByVal e As EventArgs)
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
pnlDetails.RenderControl(hw)
Using sr As StringReader = New StringReader(sw.ToString())
Using pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0F)
Using 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 Using
End Sub
Screenshots
Panel Control
Exported PDF file
Demo
Downloads