Search code examples
javascriptformsadobeacrobat

How to add JavaScript to PDF forms?


I am absolutely newbie in Javascript.

I have a form in PDF and want to add a Javascript action to it. Let's say I want an alert comes up when someone clicks on a check box.

Here is what I do:

1-first open the form in Acrobat Pro -> Tools-> Form -> edit:

enter image description here

2-then click on a checkbox-> Properties

enter image description here

and select action -> Run Java Script

enter image description here

and added this code:

    <SCRIPT language = "JavaScript"> 
         alert("Welcome to the script tag test page.")
    </SCRIPT>

enter image description here

After saving , nothing happens when I click on this checkbox. I am not sure if my Java code is wrong or I am missing something in the Acrobat Pro or something ?!


Solution

  • Welcome to scripting PDFs.

    First and foremost, Java and JavaScript are not the same. All they have in common are three characters.

    Second, JavaScript is much more than what they tell you it is when using it in webbrowsers. JavaScript consists of the Core and the application-specific extensions. Webbrowser JavaScript consists of the Core and the webbrowser-specific extensions. This is shown best in Flanagan's JavaScript, the Defnitve Guide, published by O'Reilly.

    So, Acrobat JavaScript consists of the Core and the Acrobat-specific extensions. Those are documented in the Acrobat JavaScript documentation, which is part of the Acrobat SDK, downloadable from the Adobe website.

    Then, there is the object model playing another role. This is also described in the Acrobat JavaScript documentation.

    I insist on this documentation, because that's what you need to have at hand, and you should have at least quickly read through the Guide and glanced at the Reference (which does have code samples).

    Then you will see that the code you tried — which is syntactically correct JavaScript — simply does not work in Acrobat.

    Actually, it seems that you wanted a checkbox to pop up an alert saying "Hello World" when being checked. Actually, you got to the correct event to use; using the MouseUp event for clicking is how the document object model in PDF forms works.

    One possibility to make appear the alert box would actually look like this:

    if (event.target.value != "Off") {
    app.alert("Hello World") ;
    }
    

    and that would do it…

    There would be other ways do do it when you are using another field to contain the code.