开发者

Getting notified when the user clicks a link in an embedded PDF

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

Is there a way to accomplish this?


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

In the 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;}
};

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

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..."]);

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.

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."]);

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.

But you have to be able to change the PDFs.


Your link is probably represented as a "Link annotation" inside your PDF file. Annotations in PDF can contain "Additional Actions", you could use a pdf processing software like iText (free for non commercial use) or Amyuni PDF Creator(commercial, usual disclaimer applis) to add a Javascript action to the "Additional Actions" collection of your link(s). You can invoke a page or method in your server using this Javascript code.


Client side:

<a href="yourpdf.pdf" id="pdf-link">PDF</a>

$("#pdf-link").click(function(){
   $.post('yoursite.com/phpfunctionpage/notifyPdf',
          {event:'pdf'},
          function(response){
              alert(response['message']);
          }, 'json'
   );
});

Your server side:

function notifyPdf(){
   $event = $_POST['event'];
   if ($event == 'pdf'){
     // handle notification here
     echo json_encode(array('result'=>1, 'message'=>'notifiation successful'));
   }
}


<a href="file.pdf" onclick:'javascriptFunction();'>Open the pdf file</a>

<script>

function javascriptFunction(){

}

</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜