0

I am trying to save a panel to pdf using ITextSharp. When I set a breakpoint and debug, the code stops on this line of code, however it does not give an error and just stops.

if (IsPostBack)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Quote.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();
}

In the panel I have textboxes, labels and GridView.

  • If you are failing on this specific line then you are having issues with ASP.Net and not iTextSharp yet. Can you post more of your code so we can see what's going on. – Chris Haas Jan 30 '14 at 14:57
  • @ChrisHaas - Sorry for the late reply I had to deal with responsibilities. Please check my edit on the code. – user3012159 Jan 30 '14 at 17:22
  • Your edit removed the "this line", you might want to re-highlight that. Back to my original question, if you comment out the iTextSharp stuff does it still break? – Chris Haas Jan 30 '14 at 17:29
  • code Edited. I get error " The document has no pages. ". – user3012159 Jan 30 '14 at 19:11

1 Answers1

1

You've got a couple of problems. If you're getting a The document has no pages message that means no content was added to the PDF which almost definitely means that there's a problem with the RenderControl method and nothing to do with iTextSharp. Another problem is that you are writing to the Response.OutputStream as well as outputting the literal document by using Response.Write(pdfDoc). You only need to do the former, I'm not actually sure what would happen with the latter.

The The document has no pages message also could mean that iTextSharp couldn't find anything to create content from. So you might have valid HTML but iTextSharp just doesn't know what to do with it. This probably isn't the case but I just had to mention it.

I'm going to break your code up into steps to help debug things. The first two blocks convert the control to HTML and then sanity checks it to make sure we've got something. You should inspect the contents of buf if you have any problems.

The third block creates a PDF from a MemoryStream and then dumps it into a byte array instead of writing directly to the Response.OutputStream. Although there's nothing incorrect with writing to the Response.OutputStream you will often find that you're swallowing errors and in general debugging is much harder. Once this block is done you should inspect bytes to make sure it has something.

The last block does the final push to the browser.

//Convert our control to HTML
string buf;
using (var sw = new StringWriter()) {
    using (var hw = new HtmlTextWriter(sw)) {
        Panel1.RenderControl(hw);
    }
    buf = sw.ToString();
}

//Sanity check
if (String.IsNullOrWhiteSpace(buf)) {
    throw new ApplicationException("No content found");
}

//Create our PDF and get a byte array
byte[] bytes;
using (var ms = new MemoryStream()) {
    using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f)) {
        using (var writer = PdfWriter.GetInstance(pdfDoc, ms)) {
            pdfDoc.Open();
            using (var htmlparser = new HTMLWorker(pdfDoc)) {
                using (var sr = new StringReader(buf)) {
                    htmlparser.Parse(sr);
                }
            }
            pdfDoc.Close();
        }
    }
    bytes = ms.ToArray();
}


//Output the PDF
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Quote.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Thanks Chris, however this still does not work... I have been spending hours on this and found out I need to convert my project to MVC. sounds like the HTML is the issue here and the code works fine when I create a new aspx page with some little data in it. Thank you once again. – user3012159 Jan 31 '14 at 07:35
  • Or maybe I dont have to convert to MVC, however that means I have to put all the items in a table within the panel and code each cell to copy to the PDF. – user3012159 Jan 31 '14 at 07:38
  • after exploring different ways to run the same code. I picked up the textboxs are givving the error " needs to be nested in a form with runat server, which it is ?? what does this mean ? – user3012159 Jan 31 '14 at 07:50
  • See this for handling the "XYZ must be placed inside a form tag with runat="server"". http://stackoverflow.com/a/6343859/231316 – Chris Haas Jan 31 '14 at 13:56