The JavaScript Console
Acrobat and Reader built-in tool for detecting errors, and writing and testing scripts for PDFs and other automation tasks.
Adobe Acrobat Pro is equipped with a very powerful JavaScript engine that can be used to beef up fillable forms to make them act like other software programs, with interactive dialog windows, special effects, and complex calculations. Form developers that want to take advantage of these features should familiarize themselves with the JavaScript console (also known simply as the “console” and sometimes called the “debugger”). The main uses of the console are:
Checking JavaScript code for errors
Running JavaScript code for testing
Running JavaScript code to automate form building and other PDF processes
Opening The Console
In Acrobat Pro, select Tools > JavaScript > Open then select Debugger from the toolbar. A much easier way is to simply press Ctrl + j (Windows Operating System).
Take the Acrobat Pro/JavaScript eLearning Course Acrobat Like a Pro
Does Adobe Reader Have A JavaScript Console?
I'm glad you asked. Yes it does, although there's no way to access it with the Reader User Interface (UI). Here are some methods for accessing it:
1) In user preferences (Ctrl + K), under the JavaScript category > JavaScript Debugger, select Show console on errors and messages. The console will open and display when you force a JavaScript error. And there lies the problem. How can you force a JavaScript error without a UI for creating fillable forms? This method is not really a solution, but keep reading.
2) Download and install this free JavaScript file that adds a custom menu item to Reader to open the console. Here's a video:
3) Download this PDF. Open it with Reader and click the button. The button Mouse Up action in the button runs the following simple script, that is the same script run by the menu item in the previous point:
console.show();
Using Reader to Add Form Fields
One of the restrictions for running scripts in the console with Reader is that you can't make Reader perform a function that is restricted by Reader. Reader does not restrict adding form fields however, so if you know JavaScript you can add form fields to a PDF with Reader and set their field properties for appearance, format, calculations, etc. I've been contemplating developing a "plug-in" for Reader that will allow the creation of form fields but the project always gets put on the back burner as other projects take precedence. I'm not sure what the demand would be for something like that (a plug-in that allows the user to develop fillable forms using Adobe Reader). If that's something that would interest you, please leave a note in the comment section below.
Trusted Functions
For security reasons, certain scripts can only be run from a trusted environment. That usually means you wouldn’t be able to place the script in a PDF so that it runs when someone clicks a form field, or even when they open the PDF. For example, you can write a script that sends the 1,000-page PDF to default printer and begins printing. That would be a huge security vulnerability for PDFs that are so easily distributable. You can also write a script that sends an email from the user’s default email program. Again - huge security nightmare if you could simply embed this script in a PDF and have it run successfully as soon as the PDF is opened.
You can do both of these things and many more if you create a trusted function in a JavaScript file, install the file in the application JavaScripts folder, and call the function from the PDF. The difference is that since the users put the file in the folder, they intend for the process to run and no one is fooled by malicious PDF files.
The JavaScript console is another trusted environment. You could do both of the examples, and many more, by running the script in the console. The difference here again is user intention. Since the users are running scripts in the console, they fully intend to create the results. Most scripts with security restrictions will run from the console.
Another trusted environment is the Action event (formerly known as the Batch Process). This handy Acrobat tool allows the user to process multiple PDFs by running scripts on all of them. The intention is there, since the user either has to create the Action, or import the Action file into the Action Wizard. Here are a couple of previous posts that discuss the Action Wizard:
How I Solved The Acrobat Popup Dialog/Action Dilemma
How To Create Your Own PDF Action (Batch Process) Tool
Running Scripts in The Console
Click inside the console Window. The first time you do this you might be asked if you want to enable the JavaScript console. Click Yes.
If there are any words, characters, or code in the window you can remove them by clicking the little recycle icon in the bottom left of the window frame. Alternatively, you can press Ctrl + a (select all) before you begin typing.
Type your code directly into the console window or copy and paste from a plain text editor like Notepad.
You can't use the regular Enter key to run code from the console because it acts as a carriage return and creates a new line.
If you have a separate number keypad on the right side of your keyboard you can use its Enter key to run code. If not, press Ctrl + Enter.
To run one line of code, place the cursor anywhere on the line and press the Enter key as described in the previous bullet point.
To run several lines of code you must select all of the code before pressing the number keypad Enter key or Ctrl + Enter.
After you run code, the console displays the result of that code or "returns" the result.
The console will return the last result only. In other words, you could run 10 lines of code that produce 10 separate results but the console will only display the result from the last line.