In this article I will explain how to export ASP.Net Panel control to PDF document using iTextSharp PDF Library
HTML Markup
<asp:Panel ID="pnlPerson" 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>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
 

 
Namespaces
You need to import the following namespaces.
C#
using System.Data;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

 
VB.Net
Imports System.Data
Imports iTextSharp.text
Imports System.IO
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf

 
Populating Data for Export
Below is the code to populate the data in ASP.Net Panel. I am populating data in DataTable and then binding the DataTable to Label controls.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Populate DataTable
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Age");
        dt.Columns.Add("City");
        dt.Columns.Add("Country");
        dt.Rows.Add();
        dt.Rows[0]["Name"] = "Mudassar Khan";
        dt.Rows[0]["Age"] = "27";
        dt.Rows[0]["City"] = "Mumbai";
        dt.Rows[0]["Country"] = "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(sender As Object, e As EventArgs) Handles Me.Load
   If Not IsPostBack Then
       'Populate DataTable
       Dim dt As New DataTable()
       dt.Columns.Add("Name")
       dt.Columns.Add("Age")
       dt.Columns.Add("City")
       dt.Columns.Add("Country")
       dt.Rows.Add()
       dt.Rows(0)("Name") = "Mudassar Khan"
       dt.Rows(0)("Age") = "27"
       dt.Rows(0)("City") = "Mumbai"
       dt.Rows(0)("Country") = "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

 

 
Export ASP.Net Panel to PDF
Below is the code to export ASP.Net Panel control to PDF on Export Button click.
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);
    pnlPerson.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
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)
    pnlPerson.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

 

 
Demo

 
Downloads
Download the complete source code in VB.Net and C# using the download link provided below