Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:56:47+00:00 2026-06-01T03:56:47+00:00

I am having a prblem in converting html to pdf The code which i

  • 0

I am having a prblem in converting html to pdf

The code which i used for conversion is as follows

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, "</?(a|A).*?>", "");
        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();
        }
}

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

parser.Parse(reader);

What to do?

The html text which i want to convert to pdf is as follows

<div id="print">
        <div style="width: 870px;" id="printable">


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

                                                make
                                                PN061549845
                                                SAFTY
                                            </b>
                                        </div>
                                        Belt
                                        <br />
                                        Compliance:NA
                                    </div>
                                </div>
                            </td><td valign="top" style="height:100px;width:20%;">
                                Rs.
                                <span id="grdItemList_ctl02_lblPrice">381</span>
                                /
                                Nos.
                            </td><td style="height:100px;width:20%;">Markand</td><td valign="middle" style="height:100px;width:25%;">
                                <img id="grdItemList_ctl02_imgItem" src="http://localhost:56814/Quotation/images/default.jpg" style="height:70px;width:70px;border-width:0px;" />
                            </td>
        </tr>
    </table>
</div>
            </div>
            <div style="padding-left: 14px; padding-right: 12px;" class="divBorder">
                <br />
                <table class="tableclass">
                    <tr>
                        <td width="20%">
                            <b>Payment Terms</b>
                        </td>
                        <td width="20%">
                            <b>Validity</b>
                        </td>
                        <td width="20%">
                            <b>Sales Tax</b>
                        </td>
                        <td width="20%">
                            <b>Delivery Schedule</b>
                        </td>
                        <td width="17%">
                            <b>Freight</b>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <span id="lblPaymentTerms">7 Days</span>
                        </td>
                        <td>
                            <span id="lblSalesTax">VAT 15%</span>
                        </td>
                        <td>
                            <span id="lblValidity">30 days</span>
                        </td>
                        <td>
                            <span id="lblDelivery"></span>
                            Days
                        </td>
                        <td>
                            <span id="lblFreight">Paid</span>
                        </td>
                    </tr>
                </table>
            </div>
            <div style="padding-left: 14px;" class="divBorder">
                <br />
                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.
            </div>
            <div style="padding-left: 14px; border-bottom: solid 1px #858585;" class="divBorder">
                <table width="100%">
                    <tr>
                        <td align="left">
                                                 </td>
                        <td>

                        </td>
                        <td align="right">
                            <img src="http://localhost:56814/Quotation/images/logo.jpg" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:56:48+00:00Added an answer on June 1, 2026 at 3:56 am

    try this code according to your source code and
    add some references

    using System.Configuration;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    using System.Windows.Forms;
    using System.Drawing.Printing;
    
     protected void ButtonPDF_Click(object sender, EventArgs e)
        {
            string title = "pdf chart";
            GETPDF(title, Chartpdf);  //pass the arguments to get chart
    
        }
    
    
        public void GETPDF(string title, Chart chart1)
        {
            Document pdoc = new Document(PageSize.A4, 72, 72, 82, 72);
            MemoryStream ms = new MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(pdoc, ms);
            pdoc.Open();
            Chunk c = new Chunk(title, FontFactory.GetFont("VERDANA", 25, 5, color: Color.GREEN));
            var img = Image.GetInstance(Chart(chart1));
            //Chunk c = new Chunk(divchart);
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Add(c);
            pdoc.Add(p);
            pdoc.Add(img);
            pdoc.Close();
            Response.AddHeader("Content-Type", "application/pdf");
            Response.AddHeader("Content-Disposition", "attachment;filename=Chart.pdf");
            Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            Response.End();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a problem converting a datetime which is in string format but
i having problem in converting this C# code into VB.net. The loadLecturer seem to
I’m converting an existing piece of code using Redemption (an MS Exchange library) to
I'm having a weird issue with Internet Explorer which is not converting a valid
I'm having a problem converting my program from VS2005 to VS2008. When I run
I am having no problem converting a string to a byteArray of UTF-16 encoded
I'm having a bit of a problem with converting the result of a MySQL
I am having a problem converting one of my company's scripts from csh to
I'm having a problem converting an inline assembler function to .asm file. I need
I am working on converting the EMF file to RTF file. I am having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.