Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c# - Saving Panel to PDF not working nor getting errors

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...