In this article I will explain with an example, how to export HTML string to PDF file using iTextSharp in ASP.Net.
 
 

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:
Div - For displaying data.
Button - For exporting the GridView to PDF file.
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 #F7F7F7">Customer Id</th >
            <th style="background-color:#B8DBFD;border:1px solid #F7F7F7">Name</th>
            <th style="background-color:#B8DBFD;border:1px solid #F7F7F7">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="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/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.pdf;
using iTextSharp.tool.xml;
 
VB.Net
Imports System.IO
Imports iTextSharp.text
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, 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
 
 

Possible Errors

The following error when you click on the Export Button.
A potentially dangerous Request.Form value was detected from the client.
 

Solution

 
 

Screenshots

The Form

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