简体   繁体   中英

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 hyperlinks within the PDF. We don't necessarily have to get rid of the blue text and underline, but if the user clicks on the hyperlink, it shouldn't go anywhere.

I've looked carefully through what API there is and couldn't find anything for it. I also looked through the source code, but nothing jumped out at me as something I could comment out in order to disable hyperlinks. Is there any way to disable hyperlinks contained within a PDF?

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 (ie links that take the user to another location within the document).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM