views:

3434

answers:

8

I've got an ASP.NET ashx class that retrieves data from a database, creates a PDF file using iTextSharp, and streams the PDF to the browser. The browser (IE and Firefox at least) is launching Acrobat Reader as a separate window to open the file. I'd like for it to display inline within the browser.

Is that something I can completely control from the server side? I'm already setting the Content-Type header to application/pdf, and I've tried setting Content-Disposition and Content-Length. Nothing so far has worked.

Is there another header I'm missing? Is there something in the PDF itself that tells the browser how to display it? Any other ideas?

+1  A: 

Try generating them into your page using html OBJECT.

<OBJECT WIDTH=640 HEIGHT=480>
    <PARAM NAME="SRC" VALUE="<%=filePath%>"> 
    <EMBED SRC=<%=filename.pdf%> WIDTH=1000 HEIGHT=680> 
        <NOEMBED> PDF should have displayed here!</NOEMBED> 
        </EMBED>
</OBJECT>

If you need to stream the response with an ashx instead of being able to return an aspx, I think you may be out of luck.

Otherwise, I believe the settings to show in browser or not, is completely client driven and out of your hands.

Serapth
Thanks for the suggestion. I want them to have the option of saving the PDF. Will embedding work for that?
John M Gant
@jmgant - when the PDF is embedded, the Acrobat ActiveX does have a "Save" icon in its toolbar.
ichiban
To be honest, I am not positive without testing. That said, even if it doesn't, you could easily add a Download link or button to your page.
Serapth
A: 

I think this header will do what you want

Content-type: application/pdf

Since you say that is not working, then I suspect it is a configuration setting on the client side.

Check your installed version of Adobe Acrobat. There is a setting in preferences for "Internet" and a checkbox that says "Display PDF in Browser".

--
bmb

bmb
Thanks for the response. I've set my Acrobat Reader to display inline but that doesn't seem to make a difference. I've been to other PDF pages that display inline in my browser, but the one I'm serving doesn't. So I don't think it's strictly a client issue.
John M Gant
A: 

Here is an article on using the embed tag to do it:http://blogs.adobe.com/pdfdevjunkie/2007/08/using_the_html_embed_tag_to_di.html

Fermin
A: 

If you have the budget, my company sells a set of products that includes an AJAX based image viewer that will let you view the PDF pages in line without Acrobat at all. In its simplest form, it is just a viewer, but you can layer in interactivity as you need.

plinth
+3  A: 

If you are using an ashx (web handler) try this:-

context.Response.AddHeader("content-disposition", "inline; filename=Something.pdf")
Christopher Edwards
+1 inline content-disposition should do the trick
Steve Claridge
I thought that would work, too, but it didn't. I also tried setting the content length.
John M Gant
It's working for me on IIS6, IE7, ASP.NET3.5. Hmmm... Have you tried the minimal case in a few browsers? If so I'll add my full source to the answer.
Christopher Edwards
Christopher, it would have worked for you because of your client side settings for Adobe or whatever PDF viewer you are using. If you have a different version, or have show in browser disabled, it won't work.
Serapth
If you show in browser disabled in Adobe Reader or whatever clearly it won't respect the content-disposition header from the web server. Otherwise it should.
Christopher Edwards
+1  A: 

So, I have a sample in one of my works that is what you need:

<cc1:ShowPdf ID="ShowPdf1" runat="server" BorderStyle="None"  BorderWidth="0px"
        Height="750px" Style="z-index: 103; "
        Width="750px"/>

and in server side :

  ShowPdf1.FilePath = String.Format("~/Handlers/Pdf.ashx?id={0}#view=FitH&page=1&pagemode=none&navpanes=1", myPublicationId);

I place here also some code from my PDF Handler :

Response.ContentType = "application/pdf";
    byte[] bytes = YourBinaryContent;

    using (BinaryWriter writer = new BinaryWriter(context.Response.OutputStream))
    {
        writer.Write(bytes, 0, bytes.Length);
    }

Anyway If my post doesn't seem clear to you, have a look at this sample How to Display PDF documents with ASP.NET

DaDa
Is ShowPdf a custom control? What does setting its FilePath property do?
John M Gant
Yes. FilePath instructs my control which PDF file (saved in database) to show, and how to show it passing the needed parameters
DaDa
+3  A: 

Setting the content-disposition and content-type headers should do it, but you might also need to call Response.ClearHeaders() to clear other headers that have been set.

Also, try using Fiddler to see the actual headers and content from the response and compare them to those from a site that works like you want.

tspauld
+1 for suggesting Fiddler.
bmb
+1  A: 

OK, turns out it was a stupid question, but I'm glad I asked it because I had never heard of Fiddler (which led me to the answer, which is why I'm accepting tspauld's answer). The PDF is generated by a web service that serves the file to a couple of different front-end sites. I was setting the content disposition to inline in the service, but that didn't matter, because the browser never got that header; it got the header from the front-end site (which was attachment). I changed it in the front-end site and that fixed it.

So the answer is that you have to have Content-Type=application/pdf and Content-Disposition=inline; filename=Something.pdf, as others have said.

John M Gant