In this article I will explain with an example, how to export HTML string to PDF file using iTextSharp in ASP.Net with C# and VB.Net.
The HTML string will be exported and downloaded as PDF file using iTextSharp XMLWorkerHelper library in ASP.Net with C# and VB.Net.
 
 
Download XmlWorkerHelper DLL
You can download the XmlWorkerHelper DLL from the following link.
Once downloaded you need to place the XmlWorkerHelper DLL in the BIN folder and add its reference to the project.
 
HTML Markup
The following HTML Markup consists of an HTML Table inside an HTML DIV, a Hidden Field and a Button.
The Button has been assigned a jQuery Click event handler to copy the contents of HTML DIV to the Hidden Field, so that the HTML string can be sent to the Server.
<div id = "Grid">
    <table cellspacing="0" cellpadding="2" style="border-collapse: collapse;border: 1px solid #ccc;font-size: 9pt;">
        <tr>
            <th style="background-color: #B8DBFD;border: 1px solid #ccc">Customer Id</th>
            <th style="background-color: #B8DBFD;border: 1px solid #ccc">Name</th>
            <th style="background-color: #B8DBFD;border: 1px solid #ccc">Country</th>
        </tr>
        <tr>
            <td style="width:120px;border: 1px solid #ccc">1</td>
            <td style="width:150px;border: 1px solid #ccc">John Hammond</td>
            <td style="width:120px;border: 1px solid #ccc">United States</td>
        </tr>
        <tr>
            <td style="width:120px;border: 1px solid #ccc">2</td>
            <td style="width:150px;border: 1px solid #ccc">Mudassar Khan</td>
            <td style="width:120px;border: 1px solid #ccc">India</td>
        </tr>
        <tr>
            <td style="width:120px;border: 1px solid #ccc">3</td>
            <td style="width:150px;border: 1px solid #ccc">Suzanne Mathews</td>
            <td style="width:120px;border: 1px solid #ccc">France</td>
        </tr>
        <tr>
            <td style="width:120px;border: 1px solid #ccc">4</td>
            <td style="width:150px;border: 1px solid #ccc">Robert Schidner</td>
            <td style="width:120px;border: 1px solid #ccc">Russia</td>
        </tr>
    </table>
</div>
<br />
<asp:HiddenField ID="hfGridHtml" runat="server" />
<asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick="ExportToPDF" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=btnExport]").click(function () {
            $("[id*=hfGridHtml]").val($("#Grid").html());
        });
    });
</script>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
 
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
 
 
Exporting HTML string to PDF
When the Export Button is clicked, the value of the HTML string is extracted from the Hidden Field. Then the HTML string is converted to PDF using XmlWorkerHelper class and finally the PDF is sent to the Response Stream for download.
C#
protected void ExportToPDF(object sender, EventArgs e)
{
    StringReader sr = new StringReader(Request.Form[hfGridHtml.UniqueID]);
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    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=HTML.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
}
 
VB.Net
Protected Sub ExportToPDF(sender As Object, e As EventArgs)
    Dim sr As New StringReader(Request.Form(hfGridHtml.UniqueID))
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
    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=HTML.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.Write(pdfDoc)
    Response.End()
End Sub
 
 
Error
You might receive the following error when you click on the Export Button.
A potentially dangerous Request.Form value was detected from the client.
 
 
Screenshot
HTML Table
Export HTML string to PDF file using iTextSharp in ASP.Net
 
Exported PDF
Export HTML string to PDF file using iTextSharp in ASP.Net
 
 
Demo
 
 
Downloads