18

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();
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Krishna Thota
  • 6,646
  • 14
  • 54
  • 79

3 Answers3

28

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.

Community
  • 1
  • 1
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • Thank you It helped me. Can you help me in this. [applying-styles-in-pdf-gridview-using-itextsharp](http://stackoverflow.com/questions/12085337/applying-styles-in-pdf-gridview-using-itextsharp) – Krishna Thota Aug 23 '12 at 07:17
  • 1
    @KrishnaThota I would love to, but unfortunately I don't have the answer for that one. – Alexis Pigeon Aug 23 '12 at 07:19
  • There is a label inside the panel which I'm making as PDF the Label is not being Displayed in PDF Can you help me? – Krishna Thota Aug 23 '12 at 07:31
  • How can I Open the Pdf file in New tab instead of opening it in the same tab? – Krishna Thota Aug 23 '12 at 07:43
  • As mentionned in another answer, in order to open the PDF in another tab/window, you'll have to set the target of the link to `"_blank"`. – Alexis Pigeon Aug 23 '12 at 08:12
  • I'm using asp:image button not `` tag – Krishna Thota Aug 24 '12 at 08:00
  • target='_blank' is not working for me, Its opening a blank new tab, and the pdf is opening in the same tab. Can anyone help me out to open the pdf in a new tab. Thanks – sandeep Feb 03 '14 at 08:23
  • @sandeep you should create your [own question](http://stackoverflow.com/questions/ask), with more detail, snippets of your code, etc... – Alexis Pigeon Feb 03 '14 at 08:27
  • @Alexis Pigeon : I was thinking of to create my own question, but fortunately i found this thread which worked for me. The only thing which not working is "target=_blank". People might mark it as duplicate if i ask the same question again – sandeep Feb 03 '14 at 08:29
  • @sandeep Explain then in your question that you tried the solutions proposed here, but that you don't manage to get the target='blank' thing working. People *should* not mark it as a duplicate then (in theory...) – Alexis Pigeon Feb 03 '14 at 08:36
3

Try This Code :

Action:

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

To open in new Tab/Window:

@Html.ActionLink("view pdf", "getpdf", "somecontroller", null, 
                  new { target = "_blank" })

OR

<a href="GeneratePdf.ashx?somekey=10" target="_blank">
Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
1

You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=FileName.pdf" will prompt the user (typically) with a "Save as: FileName.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile for this purpose. For example (assuming you aren't using MVC, which has other preferred options):

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=FileName.pdf");
Response.TransmitFile(Server.MapPath("~/folder/Sample.pdf"));
Response.End(); 

If you are try to open then the file in apicontroller Convert stream to bytesarray and then Fill the content

HttpResponseMessage result = null;
result = Request.CreateResponse(HttpStatusCode.OK);
FileStream stream = File.OpenRead(path);
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();           
result.Content = new ByteArrayContent(fileBytes);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "FileName.pdf";            

I think it will help you...

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Skull
  • 1,204
  • 16
  • 28