7

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DeX3
  • 5,200
  • 6
  • 44
  • 68
  • If you created the PDF, change the links inside the PDF to go via a proxy: `http://yourserver.com/proxy.php?url=http%3A//....` – mplungjan May 09 '11 at 09:58
  • This question is not about embedded programming so I have removed the tag. – uɐɪ May 09 '11 at 12:11

4 Answers4

7

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.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
  • I've been trying to do something similar to OP, and looking at http://livedocs.adobe.com/flex/3/html/help.html?content=PDF_1.html it seems like you've got it backwards. This is still for sending messages from the containing document into the PDF, and we both want it the other way around. – Clinton Pierce May 19 '11 at 15:20
  • 1
    My answer covers both directions. Read it again. – Mark Storer May 19 '11 at 17:07
  • @MarkStorer Mark, do you know by any chance how I can get the PDF to reply back to the containing document after it sends a js form submit using (this.myDoc.submitForm("http://localhost/Handler.ashx?r=2)? – Maya Dec 12 '12 at 09:22
  • It Can Be Done, but most of nobody ever does it. You need to use the HostContainer object and set up the proper handler in the HTML. If you want to send stuff from containing HTML to the PDF, you need a similar setup on that end. http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf, page 486. – Mark Storer Jan 08 '13 at 22:53
  • @MarkStorer i want to open a link inside a pdf in new window. Is this code work ? can any one guarantee plz. I am searching all over the internet for a solution how to detect a link inside a pdf document. – Sameera Chathuranga Apr 11 '14 at 05:19
1

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.

yms
  • 10,361
  • 3
  • 38
  • 68
-2

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'));
   }
}
Atticus
  • 6,585
  • 10
  • 35
  • 57
  • I think you misunderstood the question. OP wants to be notified whenever the user clicks a link **within** the PDF document. – Felix Kling May 09 '11 at 09:38
  • wow didnt realize it was simply that.. i thought like an email notification letting the owner know his pdf was accessed haha – Atticus May 09 '11 at 09:40
-3
<a href="file.pdf" onclick:'javascriptFunction();'>Open the pdf file</a>

<script>

function javascriptFunction(){

}

</script>
Joe
  • 1,747
  • 3
  • 17
  • 24
  • 1
    I think you misunderstood the question. OP wants to be notified whenever the user clicks a link **within** the PDF document. – Felix Kling May 09 '11 at 09:39