如何禁用PDF.js呈现的PDF中的超链接 [英] How to disable hyperlinks within a PDF rendered by PDF.js

查看:804
本文介绍了如何禁用PDF.js呈现的PDF中的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究PDF.js以便在网络应用中使用。到目前为止,它满足了我们所有的业务需求。但是,管理层要求我们能够禁用PDF中的超链接。我们不一定要摆脱蓝色文本和下划线,但如果用户点击超链接,它不应该去任何地方。

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.

我仔细查看过哪些API,但找不到任何内容。我也查看了源代码,但没有任何东西跳出来作为我可以注释掉的东西,以便禁用超链接。有没有办法禁用PDF中包含的超链接?

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();
    }
   }
};

这是做渲染页面,遍历该页面上的所有超链接,并禁用它们。我还添加了一个布尔值变量,允许您选择允许或禁止内部链接(即将用户带到文档中其他位置的链接t)。

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

这篇关于如何禁用PDF.js呈现的PDF中的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆