使用itextsharp导出为pdf

时间:2013-05-07 13:32:46

标签: itextsharp stringbuilder

我有一个.aspx页面,其中有一个面板,一些控件和一个gridview。我正在尝试打印该面板,但下载的pdf为空。我不知道为什么,有人可以解释一下吗?

这是我正在尝试的代码

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.pnlprint.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();

1 个答案:

答案 0 :(得分:0)

尝试更改您的:

Response.Write(pdfDoc);

为:

Response.BinaryWrite(pdfDoc.ToArray());