Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed a PDF on a ASP.NET page

Tags:

asp.net

pdf

Im trying to embed a PDF on to my page, but the PDF just does not load. I have checked the file path which is correct.

When the page first loads, I get a blank grey embed field, however when I click on the embed, I get this (which stays like this):

Loading remains like this

<script>
    var selected_doc_ref = "";
    function getPDF() {

        //selected_doc_ref = "395";

        var DV_pdf_path = "../../../Document_Viewer/Risk_Assessment/RISK ASSESSMENT 1024.pdf";

        var pdf = document.getElementById("pdf");
        var clone = pdf.cloneNode(true);
        clone.setAttribute('src', DV_pdf_path);
        pdf.parentNode.replaceChild(clone, pdf)
    }
</script>


<body onload="getPDF()">

<embed id="pdf" style="border-radius: 10px;position: relative;top:0;right:0;left:0;bottom:0;width:100%;height:620px;"/>

Any ideas where I am going wrong?

Thanks

like image 323
dynamicuser Avatar asked Jan 21 '26 06:01

dynamicuser


1 Answers

As the browser does not refresh the embed tag when you add the src attribute to the <embed> tag after page load you have 2 options:

  • Set the src attribute on the <embed> tag directly so it is available from the beginning on. You could set the src through code behind or as a static value.
  • In your javascript, add the whole <embed> tag inside the getPDF() function instead of just the attribute src:

    var e = document.createElement('embed');
    e.attributes['src'] = src;
    e.attributes['style'] = "border-radius: 10px;position: relative;top:0;right:0;left:0;bottom:0;width:100%;height:620px;";
    document.getElementById("pdfContainer").appendChild(e);
    

Assuming that you have a element with id "pdfContainer" where you want to place the <embed> tag inside.

like image 99
RononDex Avatar answered Jan 22 '26 20:01

RononDex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!