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;
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:
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: \"" + this.info.Title + "\"\n";
msg = msg + "Author: " + this.info.Author + "\n";
msg = msg + "Subject: " + this.info.Subject + "\n";
app.alert(msg,3);
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:
To create the document level JavaScript, go to menu View -> Tools -> JavaScript and click on "Document JavaScript" to add it:
Name the script as "start", for example, clear the suggested script (function...) and add the JavaScript bellow:
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);