打开PDF的浏览器,而不是下载它的 [英] Opening a PDF in browser instead of downloading it

查看:128
本文介绍了打开PDF的浏览器,而不是下载它的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp的打印面板插入按钮点击PDF。点击按钮之后,将PDF被下载到客户端的计算机。取而代之的是我需要的PDF在浏览器中,而不是下载打开。来自浏览器的用户将能够在PDF下载到他的计算机。

我用下面的code:

  Response.ContentType =应用程序/ PDF
Response.AddHeader(内容处置,附件;文件名=+文件名+.PDF);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter的SW =新的StringWriter();
HtmlTextWriter的HW =新的HtmlTextWriter(SW);
pnl_print.RenderControl(HW);StringReader SR =新StringReader(sw.ToString());
文档pdfDoc =新的文档(PageSize.A4,10F,10F,100F,0F);
HTMLWorker的HTMLParser =新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();


解决方案

内容处置更改为在线而不是附件

那么你的代码片段的第二行是

  Response.AddHeader(内容处置,内联;文件名=+文件名+.PDF);

请参阅Content-Disposition:What是的&QUOT的差异;内联"和"附件"?进一步的细节。

I'm using iTextSharp to print a panel into PDF on button click. After clicking on the button, the PDF is downloading to the client's computer. Instead of this I need the PDF to be opened in a browser instead of downloading. From the browser the user will be able to download the PDF to his PC.

I'm using the following code:

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

解决方案

Change the content-disposition to inline instead of attachment.

The second line of your snippet would then be

Response.AddHeader("content-disposition", "inline;filename=" + filename + ".pdf");

See Content-Disposition:What are the differences between "inline" and "attachment"? for further details.

这篇关于打开PDF的浏览器,而不是下载它的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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