JavaScript Support

The PDF specification includes JavaScript support based on JavaScript version 1.5 of ISO-16262 (formerly known as ECMAScript). The PSPDFKit framework uses its own JavaScript core to support some of these JavaScript features.

Usage

There are many places in a PDF where JavaScript can be used:

  • Document-level scripts are executed when the document’s JavaScript runtime initializes.
  • PSPDFJavaScriptAction can be set anywhere actions are set (any annotation or form element).

Since actions can be attached to any interactive element, JavaScript can be very ubiquitous. Calculating and formatting numbers in PDF forms is one of the largest use cases for JavaScript support.

Note: When processing or modifying a document, be sure to use at least PSPDFKit 7.0.3 for iOS, which ensures that document-wide JavaScript entries are preserved.

Disable JavaScript

JavaScript can be disabled per document via the following call. Set this before the document is displayed:

1
document.javaScriptEnabled = false
1
document.javaScriptEnabled = NO;

Supported Features

PSPDFKit has basic support for the most common JavaScript features, such as:

  • alert
  • pageNum
  • numFields
  • getField
  • getNthFieldName
  • print
  • mailDoc
  • resetForm
  • alert (Basic alert, no parameters)
  • buttonImportIcon
  • launchURL

Furthermore, most color and console features are present, along with various formatting options:

  • AFNumber_Format, AFMergeChange, AFNumber_Keystroke, AFNumber_Format, AFMakeNumber, AFDate_FormatEx, AFParseDateOrder, AFParseDateEx, AFDate_KeystrokeEx, AFParseTime, AFTime_Keystroke, AFTime_Format, AFTime_FormatEx, AFPercent_Format, AFSpecial_Keystroke, AFSpecial_Format, AFSimple, AFSimpleInit, AFSimple_Calculate.

JavaScript support is an experimental feature, and as such, not officially supported, so methods might be incomplete or return unexpected results. You are advised to test your JavaScript-enabled documents thoroughly. Please ping us if you have a document that isn’t quite working, and we’ll see what we can do.

Debugging

To see all JavaScript used in a document, open Adobe Acrobat, select Prepare Form, click on the small down arrow on the sidebar, and choose All JavaScripts. A window that shows all JavaScript used on a certain page will then open.

Learn More