i am opening my pdf which is in local folder in iframe I want to disable printing option in it like user should not able to print or save or right click the pdf but when i disable it, the scrolling inside the iframe is not working

Ashraf Khan 40 Reputation points
2025-03-17T05:30:47.0766667+00:00

function openBook(bookId, bookPath) {

// Encode the bookId and bookPath to avoid URL issues

var encodedBookId = encodeURIComponent(bookId);

var encodedBookPath = encodeURIComponent(bookPath);

// Set the iframe source to load the book

var readOnlineUrl = "/Home/ReadOnline?id=" + encodedBookId + "&path=" + encodedBookPath + "#toolbar=0&scrollbar=0";

// Set the iframe's source to the generated URL

var iframe = document.getElementById("ReadOnlineframe");

iframe.src = readOnlineUrl;

// Show the iframe container and loading indicator

document.getElementById("divReadOnlineIframe").style.display = "block";

document.getElementById("Frameloader").style.display = "block";

// Show the modal

$('#mdlReadOnline').modal("show");

// Optional: Hide loader once iframe is loaded

iframe.onload = function () {

    document.getElementById("Frameloader").style.display = "none"; // Hide loader after content is loaded

    // Access the iframe's content window and document

    var iframeDocument = iframe.contentWindow.document;

    // Add custom styles directly to the iframe's document to ensure scrolling

    var style = iframeDocument.createElement('style');

    style.innerHTML = `

        body {

            user-select: none; /* Disable text selection */

            pointer-events: none; /* Disable all mouse interactions */

            height: 100%; /* Ensure body stretches to full height */

            margin: 0; /* Remove default margin */

            padding: 0; /* Remove padding */

        }

    `;

    iframeDocument.head.appendChild(style); // Append the style to the iframe's head

    // Ensure the iframe itself is scrollable if the content overflows

    iframe.style.overflow = 'auto';  // Allow iframe to scroll

    // Disable right-click on the iframe's document

    iframeDocument.addEventListener('contextmenu', function (e) {

        e.preventDefault(); // Disable right-click menu

    });

};

}

and my cshtml script
<iframe id="ReadOnlineframe" onload="success()" height="700" width="100%" src="/Home/ReadOnline/?id=" + encodedBookId +"#toolbar=0&scrollbar =0"></iframe>

<script>

// Disable Print, Save, and Developer Tools

document.addEventListener("keydown", function (e) {

    if (e.ctrlKey && (e.key === "p" || e.key === "P")) {

        e.preventDefault();

        alert("Printing is disabled.");

    }

    if (e.ctrlKey && e.key === "s") {

        e.preventDefault();

        alert("Saving is disabled.");

    }

    if (e.ctrlKey && e.shiftKey && e.key === "I") {

        e.preventDefault();

        alert("Developer tools are disabled.");

    }

    if (e.ctrlKey && e.key === "u") {

        e.preventDefault();

        alert("Viewing page source is disabled.");

    }

});

// Prevent Print Dialog

window.addEventListener("beforeprint", function (event) {

    event.preventDefault();

    alert("Printing is disabled.");

});

// Disable Print from Context Menu

//document.addEventListener("contextmenu", function (e) {

//    e.preventDefault();

//    alert("Right-click is disabled.");

//});

// Disable Print for PDF in Iframe

document.getElementById('pdfIframe').contentWindow.document.addEventListener("keydown", function (e) {

    if (e.ctrlKey && (e.key === "p" || e.key === "P")) {

        e.preventDefault();

        alert("Printing is disabled.");

    }

});

// Add CSS dynamically to prevent Razor parsing issues

var style = document.createElement("style");

style.innerHTML = "@@media print { body { display: none !important; } }";

document.head.appendChild(style);

document.getElementById("myIframe").addEventListener("mouseover", function () {

    this.style.pointerEvents = "auto";

});

document.getElementById("myIframe").addEventListener("mouseout", function () {

    this.style.pointerEvents = "none";

});

</script>image

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.
11,400 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 49,831 Reputation points Microsoft External Staff
    2025-03-17T08:23:17.29+00:00

    Hi @Ashraf Khan , Welcome to Microsoft Q&A,

    Your problem seems to be an HTML problem.

    Because pointer-events: none; prevents the iframe from receiving mouse scroll events, scrolling is disabled.

    If you can use the sandbox attribute. sandbox="allow-scripts" can prohibit printing, downloading and right-clicking within the iframe, allowing scrolling and JavaScript (but not accessing external pages). But this method seems to only work for local PDFs.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.