【问题标题】:ASP.NET & PDF FilesASP.NET 和 PDF 文件
【发布时间】:2011-05-04 15:47:13
【问题描述】:

我正在尝试获取一个 pdf 文件,以便在我的 aspx 页面中显示一个带有 iframe 的 pdf 文档。我正在使用以下代码:

<iframe runat="server" id="testpdf" src="http://localhost:1114/pdfs/3211LD.pdf">
</iframe>

当我运行项目时,我只是得到一个空框架。但是,当我将 src (http://localhost:1114/pdfs/3211LD.pdf) 粘贴到浏览器的地址栏中时,它会要求我运行/保存文件。所以我知道我的虚拟目录设置正确。

那么为什么它不会显示在 iframe 中?我的代码有问题吗?

谢谢, 杰森

【问题讨论】:

  • 发现您必须至少运行 IE8(我的虚拟机有旧版本)和最新的 Adob​​e Reader 副本。

标签: asp.net pdf iframe


【解决方案1】:

最好使用 aspx 页面二进制文件编写 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

【讨论】:

    【解决方案2】:

    当网络浏览器显示 PDF 文件时,它实际上是使用 Adob​​e Reader 插件来呈现内容。我从未见过从 iframe 内部运行的 Adob​​e Reader 插件。我不相信它会那样工作。

    【讨论】:

    • 我也没有,但为了记录:
    • pnlPDF.Visible = True iFrameControl.ID = "PDFVeiwer" iFrameControl.Attributes.Add("width", "100%") iFrameControl.Attributes.Add("height", "400") iFrameControl .Attributes.Add("runat", "server") iFrameControl.Attributes.Add("scrolling", "auto") iFrameControl.Attributes.Add("align", "middle") iFrameControl.Attributes.Add("frameborder" , "1") iFrameControl.Attributes.Add("src", "\\MyServer\Letter_UW_3644700.pdf") pnlPDF.Controls.Add(iFrameControl)
    【解决方案3】:

    您无法完全控制浏览器将执行的操作,但如果您自己发送 PDF,您可以将 Content-disposition 标头更改为内联。遵循@BobTodd 的代码,但添加

     Response.AddHeader("Content-disposition", "inline");
    

    请求在页面中显示 PDF。

    另一种方法是将 PDF 页面光栅化为 png 并在常规网页中使用 &lt;img&gt; 标签。你可以用 GhostScript 做到这一点。

    免责声明:我在 Atalasoft 工作。我们制作了一个ASP.NET image viewer web control 和一个PDF Reader add-on,在服务器上自动转换PDF 并在网页中显示,而不需要Acrobat。

    【讨论】:

      【解决方案4】:

      您需要使用标签在页面内显示 PDF(和/或可能是嵌入标签)

      这里有一个在各个方面都有帮助的问题:Recommended way to embed PDF in HTML?

      【讨论】:

        猜你喜欢
        • 2015-12-19
        • 2022-10-24
        • 1970-01-01
        • 2010-11-06
        • 2010-12-18
        • 2011-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多