Introduction to Acrobat JavaScript

A practical guide to automating PDF workflows and building custom solutions with JavaScript in Adobe Acrobat.

← Back to Blog

What Is Acrobat JavaScript?

Acrobat JavaScript is Adobe's implementation of JavaScript specifically designed for automating tasks within Adobe Acrobat and PDF documents. Built on the core JavaScript language, it extends it with specialised objects and methods for manipulating PDF files, interacting with form fields, communicating with databases, and controlling document behaviour.

Unlike browser JavaScript that operates on HTML pages, Acrobat JavaScript operates on the PDF document model. It provides access to pages, bookmarks, annotations, form fields, and document metadata through a rich object hierarchy rooted in the app and this (document) objects.

What Can You Do with Acrobat JavaScript?

Acrobat JavaScript enables a wide range of automation and customisation tasks:

  • Form calculations: Perform arithmetic, string manipulation, and conditional logic across form fields.
  • Data validation: Check user input against patterns, ranges, or custom rules before accepting form submissions.
  • Document manipulation: Add, delete, move, and extract pages. Modify bookmarks, annotations, and metadata programmatically.
  • Batch processing: Apply operations across hundreds of PDF files using Action Wizard sequences.
  • Dynamic content: Show or hide form fields, change colours, and update text based on user interactions or data values.
  • Database connectivity: Connect to ODBC data sources to read from and write to databases.
  • Navigation: Create interactive buttons, links, and navigation systems within PDF documents.

Where JavaScript Runs in Acrobat

JavaScript code can execute in several contexts within Acrobat:

Document-Level Scripts

Scripts that run when a PDF document is opened. Useful for initialising variables, setting up calculations, or configuring the document's initial state. Access these through Tools > JavaScript > Document JavaScripts.

Page-Level Scripts

Scripts triggered when a specific page is opened or closed. Useful for page-specific actions like logging page views or updating navigation elements.

Field-Level Scripts

Scripts attached to form fields, triggered by events such as mouse enter, mouse exit, focus, blur, keystroke, format, validate, and calculate. These are the most common type of Acrobat JavaScript.

Action Wizard (Batch) Scripts

Scripts that run as part of an Action Wizard sequence in Acrobat Pro. These can process multiple files and are the primary mechanism for batch automation. Add an Execute JavaScript step to any action to run custom code.

Folder-Level Scripts

Scripts placed in Acrobat's JavaScripts folder that load automatically when Acrobat starts. These can add custom menu items, toolbar buttons, and application-level functionality. The typical location is:

C:\Program Files\Adobe\Acrobat DC\Acrobat\JavaScripts\

The Acrobat JavaScript Object Model

The key objects you will work with include:

  • app — The Acrobat application. Provides access to menus, toolbars, alerts, and global settings.
  • this — The current document (a Doc object). Access pages, fields, bookmarks, annotations, and document properties.
  • this.getField(name) — Returns a Field object for reading and setting form field values and properties.
  • this.bookmarkRoot — The root of the bookmark tree. Create, modify, and delete bookmarks.
  • event — Provides context about the current event (keystroke, mouse action, field change).

Practical Examples

Create a Bookmark for Every Page

var root = this.bookmarkRoot;
for (var i = 0; i < this.numPages; i++) {
    root.createChild("Page " + (i + 1), "this.pageNum=" + i, i);
}

Delete All Bookmarks

this.bookmarkRoot.remove();

Set a Form Field Value

var field = this.getField("totalAmount");
field.value = 1250.00;

Validate an Email Address on Field Exit

// Attach as a custom validation script on a text field
var email = event.value;
var pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (email !== "" && !pattern.test(email)) {
    app.alert("Please enter a valid email address.");
    event.rc = false;
}

Add Navigation Buttons to Every Page

for (var i = 0; i < this.numPages; i++) {
    // "Previous" button
    if (i > 0) {
        var prev = this.addField("btnPrev." + i, "button", i, [20, 30, 80, 15]);
        prev.setAction("MouseUp", "this.pageNum = " + (i - 1));
        prev.buttonSetCaption("Previous");
    }
    // "Next" button
    if (i < this.numPages - 1) {
        var next = this.addField("btnNext." + i, "button", i, [90, 30, 150, 15]);
        next.setAction("MouseUp", "this.pageNum = " + (i + 1));
        next.buttonSetCaption("Next");
    }
}

Extract Email Addresses from a Document

var report = "";
var pattern = /[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}/g;

for (var i = 0; i < this.numPages; i++) {
    var text = this.getPageNthWord(i, 0, true);
    // getPageNthWord with nWord=0 and bStrip=true returns full page text
    var matches = text.match(pattern);
    if (matches) {
        for (var j = 0; j < matches.length; j++) {
            report += matches[j] + "\n";
        }
    }
}
app.alert("Found email addresses:\n" + report);

Rotate All Landscape Pages to Portrait

for (var i = 0; i < this.numPages; i++) {
    var page = this.getPageBox("Crop", i);
    var width = page[2] - page[0];
    var height = page[1] - page[3];
    if (width > height) {
        this.setPageRotations(i, i, 90);
    }
}

Running JavaScript in Batch with Action Wizard

Action Wizard (available in Acrobat Pro) allows you to create reusable sequences of operations that can be applied to multiple files. To include custom JavaScript in an action:

  1. Open Tools > Action Wizard > Create New Action.
  2. From the tool list, add Execute JavaScript.
  3. Click the Specify Settings button to open the script editor and enter your code.
  4. Add any additional steps (save, export, etc.) and save the action.
  5. Run the action against a folder of PDF files.

This is the most practical way to apply JavaScript operations across large document sets without writing a standalone application.

Adding Custom Menu Items

You can extend Acrobat's menus by placing a .js file in the JavaScripts folder. The script runs when Acrobat launches:

app.addMenuItem({
    cName: "Count Pages",
    cParent: "Edit",
    cExec: "app.alert('This document has ' + this.numPages + ' pages.');",
    nPos: 0
});

Save this as CountPages.js in the Acrobat JavaScripts folder and restart Acrobat. A new "Count Pages" item will appear in the Edit menu.

Resources for Learning More

Need Acrobat JavaScript Development?

Mapsoft has deep expertise in Acrobat JavaScript, the Acrobat SDK, and PDF automation. Let us build your solution.