Skip to content Skip to sidebar Skip to footer

How To Disable Hyperlinks Within A Pdf Rendered By Pdf.js

I'm looking into PDF.js for use in a web app. So far, it's meeting all of our business requirements. However, management has requested that we have the ability to disable hyperlink

Solution 1:

After a great deal of experimentation, I found out how to do this by modifying the source. There is a block of code that begins with the following:

document.addEventListener('pagerendered', function (e) {

At the end of the function before the close bracket, add the following code:

var allowInternalLinks = true;
var page = document.getElementById('pageContainer' + pageNumber);
var hyperlinks = page.getElementsByTagName('a');
for (var i=0; i<hyperlinks.length; i++){
  if (!allowInternalLinks || hyperlinks[i].className != 'internalLink'){
    hyperlinks[i].onclick = function(e) {
      e.preventDefault();
    }
   }
};

What this does is take the rendered page, iterate through all of the hyperlinks on that page, and disable them. I have also added a boolean variable that allows you to optionally allow or disallow internal links (i.e. links that take the user to another location within the document).

Post a Comment for "How To Disable Hyperlinks Within A Pdf Rendered By Pdf.js"