Display PDF in iframe

后端 未结 4 812
离开以前
离开以前 2020-12-06 22:07

I have a solution in place for my site that renders a requested PDF document in a full page using the code below. Now my client wants the document rendered within an iframe

相关标签:
4条回答
  • 2020-12-06 22:21

    I actually solved this! The solution was to generate another aspx page (showpdf.aspx) with the code that renders the PDF (the meat of it being the Response... code), then call that code in the iframe. I pass the necessary variable from the source page. Thanks all!

    <iframe runat="server" id="iframepdf" height="600" width="800"  >  
    </iframe>
    
            protected void Page_Load(object sender, EventArgs e)
            {
                String encounterID = Request.QueryString["EncounterID"];
                iframepdf.Attributes.Add("src", "showpdf.aspx?EncounterID=" + Request.QueryString["EncounterID"]);
            }
    
    0 讨论(0)
  • 2020-12-06 22:23

    You need to set the src="" to a different URL that serves a PDF.

    0 讨论(0)
  • 2020-12-06 22:25

    Use an ASHX handler. The source code to your getmypdf.ashx.cs handler should look something like this:

    using System;
    using System.Web;
    
    public class getmypdf : IHttpHandler 
    {
      public void ProcessRequest(HttpContext context)
      {
            Response.ContentType = "Application/pdf";
            Response.WriteFile("myfile.pdf");
            Response.End();
      }
    
      public bool IsReusable 
      {
        get 
        {
          return false;
        }
      }
    }
    

    getmypdf.ashx would contain something like this:

    <% @ webhandler language="C#" class="getmypdf" %>
    

    And your iframe would look like this:

    <iframe runat="server" id="iframepdf" height="600" width="800" src="..../getmypdf.ashx"> </iframe>
    
    0 讨论(0)
  • 2020-12-06 22:27

    Did you try:

    window.onload = function() {
         document.getElementById("iframepdf").src = "the URL that loads the PDF"
    }
    
    0 讨论(0)
提交回复
热议问题