我正在使用 iTextSharp 在单击按钮时将面板打印成 PDF。单击按钮后,PDF 将下载到客户的计算机上。取而代之的是,我需要在浏览器中打开 PDF 而不是下载。用户可以从浏览器将 PDF 下载到他的 PC。
我正在使用以下代码:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnl_print.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();
sr.Close();
hw.Close();
sw.Close();
最佳答案
将 content-disposition
更改为 inline
而不是 attachment
。
您的代码段的第二行将是
Response.AddHeader("content-disposition", "inline;filename=" + filename + ".pdf");
参见 Content-Disposition:What are the differences between "inline" and "attachment"?了解更多详情。
关于c# - 在浏览器中打开 PDF 而不是下载它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12086321/