Itextsharp pdf export NullReferenceException

I am trying to export an aspx page to pdf. I use this code on Button2_Click, but I accept a System.NullReferenceException on htmlworker.Parse (str) ;:

string attachment = "attachment; filename=Article.pdf"; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/pdf"; StringWriter stw = new StringWriter(); HtmlTextWriter htextw = new HtmlTextWriter(stw); dvText.RenderControl(htextw); Document document = new Document(); PdfWriter.GetInstance(document, Response.OutputStream); document.Open(); StringReader str = new StringReader(stw.ToString()); HTMLWorker htmlworker = new HTMLWorker(document); htmlworker.Parse(str); document.Close(); Response.Write(document); Response.End(); 
0
c # pdf itextsharp
Jun 18 '13 at 11:36 on
source share
3 answers

Although you can write directly to Response.OutputStream , this can sometimes mask errors. Instead, I recommend that you write to another stream, such as FileStream or MemoryStream . If you use the latter, you can also save the MemoryStream to an array of bytes, which you can pass between functions. The code below shows this, and also uses a disposition template for disposable objects.

 //We'll use this byte array as an intermediary later Byte[] bytes; //Basic setup for iTextSharp to write to a MemoryStream, nothing special using (var ms = new MemoryStream()) { using (var document = new Document()) { using (var writer = PdfWriter.GetInstance(document, ms)) { document.Open(); //Create our HTML worker (deprecated by the way) HTMLWorker htmlworker = new HTMLWorker(document); //Render our control using (var stw = new StringWriter()) { using (var htextw = new HtmlTextWriter(stw)) { GridView1.RenderControl(htextw); } using (var str = new StringReader(stw.ToString())) { htmlworker.Parse(str); } } //Close the PDF document.Close(); } } //Get the raw bytes of the PDF bytes = ms.ToArray(); } //At this point all PDF work is complete and we only have to deal with the raw bytes themselves string attachment = "attachment; filename=Article.pdf"; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/pdf"; Response.BinaryWrite(bytes); Response.End(); 

The above may still break down on you depending on how you do your control. You may receive a message that says:

 Control 'xxx' of type 'yyy' must be placed inside a form tag with runat=server 

You can get around this by overriding the VerifyRenderingInServerForm page.

 public override void VerifyRenderingInServerForm(Control control) { } 
+1
Jun 18 '13 at 14:39
source share

Does your HTML contain an <hr> ? It is not supported in HTMLworker.

+1
Jun 20 '13 at 14:35
source share

I have a similar problem. I can make sure that passing HTMLTagProcessors to HTMLWorker solves this problem.

 HTMLWorker htmlworker = new HTMLWorker(document, new HTMLTagProcessors(), null); 

Now some HTML tags are supported by HTMLWorker.

0
Sep 28 '15 at 10:54
source share



All Articles