iTextSharp HTML Parse Error: Object reference not set to an instance of an object

Last Reply one month ago By pandeyism

Posted one month ago

In my project i am using itextsharp for pdf generation below is my code.

it is giving me error Object reference not set to an instance of an object

aspx.cs

        [Obsolete]
        protected void btn_download_Click(object sender, EventArgs e)
        {
            exportpdf();
        }

        [Obsolete]
        private void exportpdf()
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=OrderInvoice.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Panel1.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();
        }

error

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 304:            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
Line 305:            pdfDoc.Open();
Line 306:            htmlparser.Parse(sr);
Line 307:            pdfDoc.Close();
Line 308:            Response.Write(pdfDoc);

Source File: C:\Users\ait_0207\source\repos\ait0207(sem7)\ait0207(sem7)\farmer_wizard.aspx.cs    Line: 306

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   iTextSharp.text.html.simpleparser.HTMLWorker.CreateLineSeparator(IDictionary`2 attrs) +13
   iTextSharp.text.html.simpleparser.HTMLTagProcessor_HR.StartElement(HTMLWorker worker, String tag, IDictionary`2 attrs) +28
   iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, IDictionary`2 attrs) +84
   iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) +1616
   iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader) +78
   ait0207_sem7_.farmer_wizard.exportpdf() in C:\Users\ait_0207\source\repos\ait0207(sem7)\ait0207(sem7)\farmer_wizard.aspx.cs:306
   ait0207_sem7_.farmer_wizard.btn_download_Click(Object sender, EventArgs e) in C:\Users\ait_0207\source\repos\ait0207(sem7)\ait0207(sem7)\farmer_wizard.aspx.cs:289
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9796886
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +211
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4001.0
You are viewing reply posted by: pandeyism one month ago.