How do I add JavaScript to a PDF form?

I'm new to JavaScript. Can you tell me how I insert the JavaScript into a PDF form?

Patty Friesen

3 Answers

Voted Best Answer

Fist of all, you need Adobe Acrobat, not free Adobe Reader, to do that.

Acrobat JavaScript enables you to do a wide variety of things within a PDF document. These include:

  • Performing calculations: you can specify calculations that can be performed on several field values to yield a desired result
  • Responding to user actions: You can tie JavaScript code, or scripts, to a particular action or event such as mouse clicks, text entry, entering or exiting fields, etc
  • Validating user data: Modifying the Acrobat application: Use JavaScript to add custom menu items and Add-on Tools Controlling the behavior of the document: You can control what happens when a document first opens by using document-level scripts.
  • Dynamically modifying a document’s appearance and function:
    Modifying field properties, such as hidden, read-only, required, and don’t print
    Populating list boxes and combo boxes with different choices
  • Dynamically creating a new page based on an Acrobat page template

For example, if you need to set the calculated value of a field called "Total" to be the value of the "SubTotal" field plus tax (8% for example) you could add the following JavaScript as a Custom calculation script in the "Total" field:

var f = this.getField("SubTotal");
event.value = f.value * 1.08;

enter image description here

Another example is to add a JavaScript to button, to its Mouse Up event. You can add the following script to a button to show some information about the current PDF file:

enter image description here

var np = this.numPages;
var p = this.pageNum;

var msg = "Information about this PDF:" + "\n\n";
msg = msg + "Number of pages: " + np + "\n";
msg = msg + "Current page: " + (p + 1) + "\n";
msg = msg + "Title: \"" + + "\"\n";
msg = msg + "Author: " + + "\n";
msg = msg + "Subject: " + + "\n";


You can also create Document Level JavaScripts to define variables and functions that are global in scope to the entire document or to execute a script when the PDF form is opened.
For example, if you have a field called "currentDate" that you want to be updated whenever the document is opened, and this field is formatted as a date field in Acrobat as "mm/dd/yyyy" you can follow these steps:

Set the field as Date and choose the appropriate option:
enter image description here

To create the document level JavaScript, go to menu View -> Tools -> JavaScript and click on "Document JavaScript" to add it:

enter image description here

Name the script as "start", for example, clear the suggested script (function...) and add the JavaScript bellow:

enter image description here

The code to be added, supposing there is a field called "currentDate" on the form:

var gD = new Date(); 
this.getField("currentDate").value = util.printd("mm/dd/yyyy",gD);

enter image description here

enter image description here

By Almir R V Santos   

There are almost two dozen places in a PDF form where you can insert JavaScripts. So, a little bit more information would be needed.

The recommended reading material is the Acrobat JavaScript documentation which is part of the Acrobat SDK, downloadable from the Adobe website. Besides the Reference, which you will need at hand in any case, your questions are widely asked by the Guide.

Hope this can help.

Max Wyss.

Max Wyss   

With Acrobat Standard not having the ability to edit a document level script, one can add the code to the page open action but might not want the code to run every time the page is opened, this can arise with a multipage form, so I would use a function to update the field and also test to see if it has already been run before updating the field or preforming other initialization tasks. There are a couple of functions provided that could be reused within the form for repeated tasks.

The following is a sample script:

function GetField(cName) {
// return the field object for the named field;
var oField = this.getField(cName);
if(oField == null) app.alert("Error getting field " + cName + "\nPlease check the name.", 0,1);
return oField;

function UpdateDateField(cFormat, cName, oDateObject) {
// update a date field with a new value;
var oField = GetField(cName);
oField.value = util.printd(cFormat, oDateObject);

if(bInitialized == "undefined") var bInitialized = false;

function Initilize() {
// run once script to initialize a PDF;
if(!bInitialized) {
// place code to run once here;
UpdateDateField("d-mmm-yyyy", "MyDateField", new Date());
bInitialized = true;
// code to always run can go here;
} // end Initialize function;,

Initilize(); // run the initialize function;

The code checks to see if the form has been initialized by checking if a specific variable exist or not. If the variable does not exist, it is defined as a document level function or a variable the has the scope of the document. It is set to an initial state of "false". The Initialize function test the document level variable's value and only executes specific code of the variable is not true or the Initialize function has not been run before. As part of the function, the variable is set to "true" to indicate that Initialize function has been run at least once.

George Kaiser   

Please specify a reason: