Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a PDF in browser instead of downloading it

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();
like image 943
Krishna Thota Avatar asked Aug 23 '12 06:08

Krishna Thota


1 Answers

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.

like image 78
Alexis Pigeon Avatar answered Sep 27 '22 21:09

Alexis Pigeon