2

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 ?!

1
  • You don't need <script> tag, I don't thought that an alert works in Adobe Acrobat. Try to refer you to Javascript API doc Commented Oct 29, 2014 at 17:55

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for such a helpful answer. Now I know the difference between Java and Javascript. It would be awesome if you provided a link to the SDK and the Documents. Is there any IDE for testing and debugging the script ? something like eclipse for Java?
I added the link to the Acrobat main page of the Developer section of the Adobe website; there you have the links to the SDK and other useful documentation.
The "best" IDE you have is Acrobat Pro, with the Console. For helping with typical typing mistakes (balancing parentheses, quotes, etc.), I suggest using a good text editor, such as BBEdit (or its free brother Textwrangler) or NoteTab. Such editors have balancing, color coding etc. and you get most help if you can set JavaScript as base language for the editing.
Another comment to an IDE: Acrobat JavaScript is in the most cases very compact, and many things can even be done with a one-liner. This is because the document object model provides implicitely for all the overhead (you don't have to put it in your code, to which event you are adding the script, you select the event in the field property dialog, for example, and that takes care of where the code has to go.
Thanks! that was a great help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.