0

In my application I am required to structure the data is a particular way, so I placed a label on the page, and then in my code behind I generate a string containing HTML tags along with the data to be displayed. My code then sets the label.Text equal to the string containing all of the HTML so that it will be displayed on the page correctly after the data is generated.

This part is working ok, but I am trying to export the displayed data to a PDF using the itext library. My current code is as follows:

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=IssuedItems.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        lblResults.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();

This is the same code I used to export a gridview control in another project, I just replaced the Gridview name with lblResults in this code. When I run this I get an error at the line htmlparser.Parse(sr) saying that object reference not set to an instance of an object. I am assuming that lblResults.RenderControl isn't producing anything.

Does anyone know of a way I can write the text of the label into a PDF file with it actually using the HTML tags instead of just showing them as text? I was able to get it into a PDF file using a method involving just adding a Paragraph but it displayed all of the HTML tags.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Tenjjin
  • 13
  • 3
  • 1
    I've added "itextsharp" tag as it seem to be library you are using - please make sure it is correct tag. For future questions make sure to clearly specify at least name of libraries you are using if they are not part of .Net. – Alexei Levenkov Jul 08 '14 at 02:54
  • Although this uses iTextSharp, the problem is 100% on the ASP.Net side. Try the [solution here](http://stackoverflow.com/a/6343859/231316) which overrides the `VerifyRenderingInServerForm` method on the page. – Chris Haas Jul 08 '14 at 13:16
  • I had to override that method when exporting the contents of a gridview control; however, it has no effect here. I still get the same error with the above code while overriding that method. I also get a stack empty exception when I try to do this.Page.RenderControl instead of using the label. – Tenjjin Jul 08 '14 at 14:10
  • My previous comment wasn't correct however you'll still need to inspect the contents of `sw.ToString()`. If you have an `
    ` tag you'll get that specific exception. There might be some other tags that can cause that, too, but that's the most common.
    – Chris Haas Jul 08 '14 at 16:27
  • Thank you for your help. It did contain an
    tag, and I added sw.ToString().Replace("
    ", "
    "). It is now working. If you add your comment as an answer then I will mark it as so.
    – Tenjjin Jul 08 '14 at 18:13
  • possible duplicate of [iTextSharp HTMLWorker.ParseToList() throws NullReferenceException](http://stackoverflow.com/questions/8574174/itextsharp-htmlworker-parsetolist-throws-nullreferenceexception) – Chris Haas Jul 08 '14 at 20:11

0 Answers0