ASP.NET & PDF Files
I'm trying to get a pdf file to display a pdf document in my aspx page with an iframe. I'm using the following code:
<iframe runat="server" id="testpdf" src="http:/开发者_开发百科/localhost:1114/pdfs/3211LD.pdf">
</iframe>
When I run the project, I just get an empty frame. However, when I paste the src (http://localhost:1114/pdfs/3211LD.pdf) into the address bar of a browser, it asks me to run/save the file. So I know my virtual directory is set up right.
So why won't it display in an iframe? Is there something wrong with my code?
Thanks, Jason
Youd be better of with an aspx page binary writing the pdf...
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set the appropriate ContentType.
Response.ContentType = "Application/pdf"
'Get the physical path to the file.
Dim FilePath As String = MapPath("acrobat.pdf")
'Write the file directly to the HTTP output stream.
Response.WriteFile(FilePath)
Response.End()
End Sub
When a web broswer displays a PDF file, it is actually using an Adobe Reader plugin to render the contents. I have never seen the Adobe Reader plugin run from inside an iframe. I do not believe it works like that.
You cannot totally control what the browser will do, but if you send out a PDF yourself, you can change the Content-disposition header to inline. Follow @BobTodd's code, but add
Response.AddHeader("Content-disposition", "inline");
to request that the PDF be showed in the page.
Another way is to do it is to rasterize the PDF pages to png and use a <img>
tags inside of a regular web page. You could do this with GhostScript.
Disclaimer: I work at Atalasoft. We make an ASP.NET image viewer web control and a PDF Reader add-on that converts PDF on the server automatically and displays it in web pages without needing Acrobat.
You need to use the tag to display a PDF within the page (and/or possibly the embed tag)
Here a question that helps in various ways: Recommended way to embed PDF in HTML?
精彩评论