How to disable hyperlinks within a PDF rendered by PDF.js

久未见 提交于 2019-12-12 03:39:50

问题


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?


回答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).



来源:https://stackoverflow.com/questions/31053948/how-to-disable-hyperlinks-within-a-pdf-rendered-by-pdf-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!