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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With