保存面板为PDF没有工作,也没有收到错误 [英] Saving Panel to PDF not working nor getting errors

查看:218
本文介绍了保存面板为PDF没有工作,也没有收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存面板使用到PDF iTextSharp的。当我设置一个断点和调试中,code停在这条线code,然而它并没有给出一个错误,只是停止。

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

在面板我有文本框,标签和GridView。

In the panel I have textboxes, labels and GridView.

推荐答案

您已经有了几个问题。如果您收到一个文档没有页消息,意味着没有内容被添加到几乎肯定意味着有一个问题PDF中的 RenderControl 方法和无关iTextSharp的。另一个问题是,你正在写的 Response.OutputStream 以及通过使用的Response.Write(pdfDoc)。你只需要做的是前者,我实际上不是肯定会与后者发生什么。

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.

文档没有页消息也可能意味着iTextSharp的找不到任何要创建的内容。所以,你可能有有效的HTML,但iTextSharp的只是不知道该怎么办。这可能并非如此,但我不得不提到它。

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.

我要打断你的code分成步骤来帮助调试的事情。前两个块转换控制HTML,然后完整性检查它来确保我们已经得到的东西。如果您有任何问题,您应检查 BUF 的内容。

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.

第三块创建从的MemoryStream PDF文件,然后将其转储到一个字节数组,而不是直接写入 Response.OutputStream 。虽然没有什么不正确写作的 Response.OutputStream 你经常会发现你的错误吞咽和一般的调试更难。一旦这个块之后,你应该检查字节来确保它有一些东西。

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

这篇关于保存面板为PDF没有工作,也没有收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆