当用户单击嵌入 PDF 中的链接时收到通知 [英] Getting notified when the user clicks a link in an embedded PDF

查看:29
本文介绍了当用户单击嵌入 PDF 中的链接时收到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面中嵌入了一个 PDF,我想设置一些类似于 Javascript 回调的内容,以便在用户单击 PDF 中的链接时调用.

I got a PDF embedded in my page, and I'd like to set something like a Javascript-callback to be called whenever the user clicks a link within the PDF.

有没有办法做到这一点?

Is there a way to accomplish this?

推荐答案

如果您查看 Acrobat JS 参考 对于您想要支持的最低版本,您将看到有关 HostContainer 对象的文档.

If you check out the Acrobat JS Reference for the minimum version you want to support, you'll see documentation on the HostContainer object.

在 PDF 中:

this.hostContainer.messageHandler =
{
  onMessage: function(messageArray)
  {
    for(var i = 0; i < messageArray.length; i++)
      console.println("Message " + i + ": " + messageArray[i]);
  },
  onError: function(error, messageArray){ },
  onDisclose: function() {return true;}
};

在您的 HTML 中,假设您的 PDF 位于 <object id="thePdf"> 标签内:

In your HTML, assuming your PDF is inside an <object id="thePdf"> tag:

function messageFunc(messageArray) {
    for(var i = 0; i < messageArray.length; i++)
      alert("Message " + i + ": " + messageArray[i]);
}

document.getElementById("thePdf").messageHandler = { onMessage: messageFunc };

在您的 PDF 中,您还需要修改链接,以便它们具有 JS 操作,可以向包含的网页发布消息.这可以通过编程方式完成(因您使用的语言/库而异),也可以在 Acrobat Pro 中手动完成.

In your PDF, you'd also need to modify the links so they have a JS action that would post a message to the containing web page. This could be done programmatically (varying wildly depending on the language/library you use), or manually in Acrobat Pro.

this.hostContainer.postMessage(["urlClicked", "http://blah.blah.blah..."]);

不是很难,但没有人听说过.如果这在未来几年内在 Adob​​e 查看器 (Reader/Acrobat) 之外的任何地方都有效,我会震惊.

Not terribly hard, but no one's ever heard of it. I'd be stunned if this worked anywhere outside an Adobe viewer (Reader/Acrobat) for the next several years.

如果出于某种原因,您想从 HTML 向 PDF 发送消息,可以这样操作:

If you want to send a message from your HTML to the PDF for some reason, it goes something like this:

var thePDF = document.getElementById("thePdf");
thePDF.postMessage(["This", "is", "an", "array", "too."]);

您甚至可以拉出所有现有链接,并让 PDF 请求通过包装 HTML 页面打开链接……这样您就可以给新窗口命名,从 JS 中关闭它们,等等.您可以停下来正确的时髦.

You could even yank out all the existing links and have the PDF request the links be opened by the wrapping HTML page... that way you can give the new windows names, close them from JS, etc. You can get down right snazzy.

但是您必须能够更改 PDF.

But you have to be able to change the PDFs.

这篇关于当用户单击嵌入 PDF 中的链接时收到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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