Convert HTML To pdf using itextsharp in asp.net
<p>I am having a prblem in converting html to pdf</p>

<p>The code which i used for conversion is as follows</p>

<pre><code>using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


protected void ConvertToPDFNow()
{
  StringWriter sw = new StringWriter();
        HtmlTextWriter w = new HtmlTextWriter(sw);
        print.RenderControl(w);
        string htmWrite = sw.GetStringBuilder().ToString();

Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        htmWrite = Regex.Replace(htmWrite, "&lt;/?(a|A).*?&gt;", "");
        htmWrite = htmWrite.Replace("\r\n", "");
        StringReader reader = new StringReader(htmWrite);

        Document doc = new Document(PageSize.A4);
        HTMLWorker parser = new HTMLWorker(doc);
        PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        try
        {
                       parser.Parse(reader);
        }
        catch (Exception ex)
        {                   }
        finally
        {
            doc.Close();
        }
}
</code></pre>

<p>I am passing an html text which has image, table, inline-styles, id.
While converting i am getting an error "Object referenced not set to an instance" at </p>

<p>parser.Parse(reader);</p>

<p>What to do?</p>

<p>The html text which i want to convert to pdf is as follows</p>

<pre><code>&lt;div id="print"&gt;
        &lt;div style="width: 870px;" id="printable"&gt;


            &lt;div id="dvSoftCopy" style="display: none;"&gt;
                &lt;img src="http://localhost:56814/Quotation/images/head.jpg" style="width: 100%" /&gt;
            &lt;/div&gt;
            &lt;div id="dvHardCopy" style="height: 197.29px;"&gt;
                &amp;nbsp;
            &lt;/div&gt;
            &lt;div style="display: none;"&gt;
                &lt;table width="100%"&gt;
                    &lt;tr&gt;
                        &lt;td style="width: 100%; height: 3px;"&gt;
                            &lt;hr style="height: 3px; color: Blue;" /&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;
            &lt;/div&gt;
            &lt;div class="divBorder"&gt;
                &lt;table style="color: Blue; font-size: 11px; padding-left: 14px;"&gt;
                    &lt;tr&gt;
                        &lt;td colspan="2" style="width: 50%;"&gt;
                            To
                        &lt;/td&gt;
                        &lt;td&gt;
                            Quote No
                        &lt;/td&gt;
                        &lt;td style="padding-left: 5px;"&gt;
                            &lt;span id="lblQuoteNo" style="font-weight:bold;"&gt;00005&lt;/span&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td colspan="2"&gt;
                            The
                            &lt;span id="lblDesignation"&gt;nj&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            Quote Date
                        &lt;/td&gt;
                        &lt;td style="padding-left: 5px;"&gt;
                            26/04/2012
                        &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td colspan="2"&gt;
                            &lt;span id="lblPartyName"&gt;PK&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            Inquiry Date
                        &lt;/td&gt;
                        &lt;td style="padding-left: 5px;"&gt;
                            &lt;span id="lblEnquiryDate"&gt;09/09/2010&lt;/span&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td colspan="2"&gt;
                            &lt;span id="lblAddress"&gt;bhatt&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            Kind Attentions
                        &lt;/td&gt;
                        &lt;td style="padding-left: 5px;"&gt;
                            &lt;span id="lblContactPerson"&gt;123&lt;/span&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;
                            &lt;span id="lblPCity"&gt;AHMEDABAD&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            &lt;span id="lblPPostalCode"&gt;333333&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            Phone
                        &lt;/td&gt;
                        &lt;td style="padding-left: 5px;"&gt;
                            &lt;span id="lblPPhone"&gt;787878787&lt;/span&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;
                            &lt;span id="lblState"&gt;GUJARAT&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td colspan="3"&gt;
                            &lt;span id="lblCountry"&gt;PK&lt;/span&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;
            &lt;/div&gt;
            &lt;div class="divBorder"&gt;
                &lt;table style="font-size: 11px; padding-left: 14px;"&gt;
                    &lt;tr&gt;
                        &lt;td&gt;
                            Dear Sir/Madam
                            &lt;br /&gt;
                            Thank you for your inquiry, we wish to quote our lowest for the following items.
                        &lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;
            &lt;/div&gt;
            &lt;div style="padding-left: 14px; padding-right: 14px;" class="divBorder"&gt;
                &lt;div&gt;
    &lt;table class="datalist" cellspacing="0" rules="all" border="1" id="grdItemList" style="width:100%;border-collapse:collapse;"&gt;
        &lt;tr&gt;
            &lt;th scope="col"&gt;Item&lt;/th&gt;&lt;th scope="col"&gt;Price/Unit&lt;/th&gt;&lt;th scope="col"&gt;Remarks&lt;/th&gt;&lt;th align="center" scope="col"&gt;Image&lt;/th&gt;
        &lt;/tr&gt;&lt;tr style="height:100px;"&gt;
            &lt;td style="width:45%;"&gt;
                                &lt;div style="position: relative; height: 150px;"&gt;
                                    &lt;div style="position: absolute;"&gt;
                                        &lt;div style="text-transform: uppercase; font-size: 13px;"&gt;
                                            &lt;b&gt;
                                                1.

                                                make
                                                PN061549845
                                                SAFTY
                                            &lt;/b&gt;
                                        &lt;/div&gt;
                                        Belt
                                        &lt;br /&gt;
                                        Compliance:NA
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/td&gt;&lt;td valign="top" style="height:100px;width:20%;"&gt;
                                Rs.
                                &lt;span id="grdItemList_ctl02_lblPrice"&gt;381&lt;/span&gt;
                                /
                                Nos.
                            &lt;/td&gt;&lt;td style="height:100px;width:20%;"&gt;Markand&lt;/td&gt;&lt;td valign="middle" style="height:100px;width:25%;"&gt;
                                &lt;img id="grdItemList_ctl02_imgItem" src="http://localhost:56814/Quotation/images/default.jpg" style="height:70px;width:70px;border-width:0px;" /&gt;
                            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
&lt;/div&gt;
            &lt;/div&gt;
            &lt;div style="padding-left: 14px; padding-right: 12px;" class="divBorder"&gt;
                &lt;br /&gt;
                &lt;table class="tableclass"&gt;
                    &lt;tr&gt;
                        &lt;td width="20%"&gt;
                            &lt;b&gt;Payment Terms&lt;/b&gt;
                        &lt;/td&gt;
                        &lt;td width="20%"&gt;
                            &lt;b&gt;Validity&lt;/b&gt;
                        &lt;/td&gt;
                        &lt;td width="20%"&gt;
                            &lt;b&gt;Sales Tax&lt;/b&gt;
                        &lt;/td&gt;
                        &lt;td width="20%"&gt;
                            &lt;b&gt;Delivery Schedule&lt;/b&gt;
                        &lt;/td&gt;
                        &lt;td width="17%"&gt;
                            &lt;b&gt;Freight&lt;/b&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;
                            &lt;span id="lblPaymentTerms"&gt;7 Days&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            &lt;span id="lblSalesTax"&gt;VAT 15%&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            &lt;span id="lblValidity"&gt;30 days&lt;/span&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            &lt;span id="lblDelivery"&gt;&lt;/span&gt;
                            Days
                        &lt;/td&gt;
                        &lt;td&gt;
                            &lt;span id="lblFreight"&gt;Paid&lt;/span&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;
            &lt;/div&gt;
            &lt;div style="padding-left: 14px;" class="divBorder"&gt;
                &lt;br /&gt;
                In case of any futher queries please feel free to contact us by E-mail or Phone.
                Thanking you and looking forward to the opportunity to serve your esteemed Organisation.
            &lt;/div&gt;
            &lt;div style="padding-left: 14px; border-bottom: solid 1px #858585;" class="divBorder"&gt;
                &lt;table width="100%"&gt;
                    &lt;tr&gt;
                        &lt;td align="left"&gt;
                                                 &lt;/td&gt;
                        &lt;td&gt;

                        &lt;/td&gt;
                        &lt;td align="right"&gt;
                            &lt;img src="http://localhost:56814/Quotation/images/logo.jpg" /&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td colspan="2"&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
</code></pre>
