printing a pdf file embedded on web page

Anjali Agarwal 1,366 Reputation points
2023-03-20T23:26:04.0566667+00:00

I have a pdf file embedded on a web page. I want to put a print button the web page so that the end user can print the pdf file. How can I achieve this. below is the code for embedding the pdf file on web browser:

using (MemoryStream stream = new MemoryStream())

        {

            string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"1000px\" height=\"1000px\">";

            embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";

            embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";

            embed += "</object>";

            TempData["Embed"] = string.Format(embed, "/FilledPDFFiles/Confidentiality Statement" + ".pdf");

            return View();

        }

any help will be appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes

Accepted answer
  1. Farid Uddin Kiron MSFT 456 Reputation points Microsoft Vendor
    2023-03-21T08:39:26.3266667+00:00

    I have a pdf file embedded on a web page. I want to put a print button the web page so that the end user can print the pdf file. How can I achieve this. below is the code for embedding the pdf file on web browser

    If you look into your embedded object it has data property that containing your pdf file source location which we can see as following:

    PrintButton

    We ought to extract data value from HTML object element and finally need to pass that source location within new content window that will particularly invoked print prompt window.

    Let's take a look in action:

    Set a button:

    <br />
    <input type="button"
           onclick="printEmbeddedObject()"
           class="btn btn-success"
           value="Print PDF" />
    

    Note: I have set a onclick function will be invoked while button click event fired.

    Script

    <script>
        let printEmbeddedObject = () => {
         
            const getObjectElement = document.querySelector('object');
            let pdfFileLocation = getObjectElement.getAttribute('data');
            console.log(pdfFileLocation);
           
            let instanceIframeObject = document.createElement('iframe');  
            instanceIframeObject.style.visibility = 'hidden';
            instanceIframeObject.src = pdfFileLocation;
    
            document.body.appendChild(instanceIframeObject);
    
            instanceIframeObject.contentWindow.focus();
            instanceIframeObject.contentWindow.print();
        }
    
    </script>
    
    

    Note: Here, I am extracting embedded object data value and creating a virtual DOM to prompt print dialog. Finally passing the source location which got extracted from onclick event.

    Output:

    PrintLatestPDFggg

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful