How to PDF Form Javascript

JavaScript in PDF forms enables dynamic behavior far beyond simple calculations and validation — auto-populating fields from dropdown selections, showing/hiding sections conditionally, formatting input as the user types, integrating with web services, and creating wizard-like multi-step form experiences. SublimePDF lets you attach JavaScript actions to form events and document-level triggers.

Follow the step-by-step instructions below, then use the free tool directly — no registration or download required.

Open Tool →

How to PDF Form Javascript — Step by Step

1

Understand PDF JavaScript events

PDF forms support events on fields: onFocus (field selected), onBlur (field deselected), onChange (value changed), onKeypress (each keystroke). Document-level events include onOpen (PDF opened), onSave (before save), and onPrint (before printing).

2

Attach scripts to form fields

In your HTML template, use data attributes: <input name='state' data-on-change='handleStateChange(event.value)'> or <select name='country' data-on-change='populateStates(event.value)'>. SublimePDF maps these to PDF JavaScript actions.

3

Define document-level functions

Pass reusable functions in the 'formScripts' API parameter: { "formScripts": "function populateStates(country) { var states = { 'US': ['CA','NY','TX'], 'CA': ['ON','BC','QC'] }; ... }" }. These functions are available to all field scripts.

4

Show and hide form sections

Toggle field visibility based on user input: function toggleSection(show) { getField('optionalAddress').display = show ? display.visible : display.hidden; }. Use this for conditional form sections that appear only when a checkbox is ticked.

5

Format input in real time

Use onKeypress handlers to format input as the user types — add dashes to phone numbers, uppercase specific fields, or restrict input to certain characters: data-on-keystroke='event.change = event.change.toUpperCase()' for an all-caps field.

Pro Tips

  • 💡 PDF JavaScript is based on Acrobat's JavaScript API, not browser DOM APIs. There's no document.getElementById() — use getField('fieldName') to access form fields and this.getField() for the current field.
  • 💡 Keep scripts small and focused. Complex application logic belongs in your backend — the PDF form should handle UI concerns (formatting, visibility, basic validation) and submit data to your server for processing.
  • 💡 Test all JavaScript in Adobe Acrobat Reader — it has the most complete PDF JavaScript engine. Foxit Reader supports a subset. Browser viewers execute almost no PDF JavaScript.
  • 💡 Use app.alert('message') for debugging instead of console.log(). The Acrobat JavaScript console (Ctrl+J) shows errors and lets you run JavaScript interactively.

Privacy & Security

All processing happens directly in your browser. Your files are never uploaded to any server — they remain on your device throughout the entire process. SublimePDF uses WebAssembly technology for fast, secure, client-side processing.

Works Everywhere

This tool works on any modern browser — Chrome, Firefox, Safari, or Edge — on desktop, tablet, or mobile. No software to install. PDF is an open ISO standard supported by all major platforms.

How to PDF Form Javascript — FAQ

Is PDF JavaScript the same as browser JavaScript?
No. PDF JavaScript uses Acrobat's proprietary API. Core language features (variables, functions, loops, conditionals) are the same, but DOM APIs don't exist. You use getField(), app.alert(), and event objects specific to PDF.
Can PDF JavaScript make HTTP requests?
Adobe Acrobat supports SOAP web services via app.SOAP but not modern REST APIs or fetch(). For web integration, use a submit button that sends form data to your server endpoint via HTTP POST, and handle the logic server-side.
How do I debug JavaScript errors in a PDF?
Open the JavaScript console in Adobe Acrobat (Ctrl+J or Cmd+J). Errors appear here with line numbers. You can also type JavaScript commands directly into the console to test field access and function behavior.
Will JavaScript work if the PDF is opened in a browser?
No. Browser-based PDF viewers (Chrome, Firefox, Safari) do not execute PDF JavaScript. Only dedicated PDF applications (Adobe Reader, Foxit) support it. If your audience uses browser viewers, rely on HTML-based forms instead.

Ready to get started?

Use SublimePDF's free tools right now.

Open Tool