---
title: "JavaScript support in PDF viewer | Nutrient SDK"
canonical_url: "https://www.nutrient.io/guides/web/features/javascript/"
md_url: "https://www.nutrient.io/guides/web/features/javascript.md"
last_updated: "2026-06-08T09:14:14.485Z"
description: "JavaScript support in PDF viewer | guide for Nutrient Web SDK with detailed instructions and code examples."
---

# JavaScript Support in our PDF viewer

PDF JavaScript doesn’t run when [Web SDK](https://www.nutrient.io/guides/web.md) is used with [Document Engine](https://www.nutrient.io/guides/document-engine.md). If your viewer configuration sets [`documentId`](https://www.nutrient.io/api/web/interfaces/Configuration.html#documentid), `authPayload`, or `session` (typically alongside `serverUrl`), no PDF JavaScript will execute — including form field calculate, format, validate, and keystroke scripts, document-level scripts, and any `JavaScriptAction` attached to annotations. PDF JavaScript is supported only when Web SDK is loaded in standalone mode. For more information, refer to the [operational mode](https://www.nutrient.io/guides/web/about/operational-modes.md) guide.








PDF JavaScript is currently supported in our iOS and Android SDKs, as well as in Nutrient Web SDK and Nutrient Electron SDK.

In Nutrient Web SDK and Nutrient Electron SDK, JavaScript support is enabled by default.

To disable JavaScript support, set `NutrientViewer.Options.PDF_JAVASCRIPT` to `false` right before loading Nutrient:

```js

NutrientViewer.Options.PDF_JAVASCRIPT = false;
NutrientViewer.load(configuration);

```

See also: [How to program a calculator in a PDF](https://www.nutrient.io/blog/how-to-program-a-calculator-pdf/)

## Security considerations

PDF JavaScript is part of the PDF specification (ISO 32000) and is standard behavior in PDF readers like Adobe Acrobat. It’s different from browser JavaScript.

### PDF JavaScript vs. browser JavaScript

PDF JavaScript in Nutrient runs in a sandboxed environment, separate from the browser, and it cannot access:

- Browser DOM, cookies, or local storage

- Your web application’s JavaScript context

- Network resources outside the PDF viewer

Browser JavaScript has full access to the page and can cause cross-site scripting (XSS) vulnerabilities.

### Is PDF JavaScript a security risk?

No. Functions like `app.alert()` are standard PDF APIs. Nutrient runs them in a sandboxed environment, and they have no access to your application data and cannot cause XSS vulnerabilities.

### When to disable PDF JavaScript

Disable PDF JavaScript in these scenarios:

- Security-hardened environments with strict policies

- Applications that don’t use PDF forms with calculations or validations

- Regulatory compliance requirements

Disabling PDF JavaScript will break functionality in PDFs that use form field calculations, form field validations, or dynamic form behavior.

## Use

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

**Document-level scripts:** These scripts are executed when the document is open. To add a new document-level script in Adobe Acrobat, go to Tools and search for Document JavaScripts. A dialog box will open, which is where you can add a new document-level script to the PDF file.

You can retrieve the document in a document-level script by accessing the `event.target` property, like this:

```js

var document = event.target;
var textField = document.getField('MyTextField');
// Do things with `textField`.

```

This code snippet will get a reference to the form field with the name `MyTextField` in the document.

Document-level scripts are useful for reusing information and functions that will be accessed from several action scripts. For more information about document-level scripts, read the Doc/Open subsection on page 363 of the [JavaScript™ for Acrobat® API Reference](https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/index.html) guide.

**Action Scripts:** Form fields and annotations can be extended with JavaScript actions. A JavaScript action is represented by the class `JavaScriptAction`. You can initialize a  [`PSPDFKit.Actions.JavaScriptAction`](https://www.nutrient.io/api/web/PSPDFKit.Actions.JavaScriptAction.html) with a script and associate it with a form element and a particular trigger event. For more information about PDF actions, read our [PDF actions](https://www.nutrient.io/../../annotations/pdf-actions/) guide.

You can set regular action scripts in Adobe Acrobat too. Open the Prepare Form tool and double-click on a form field. Click on the Actions tab and choose Run a JavaScript from the Select Action dropdown box.

One interesting set of JavaScript actions is that of the format, validation, and calculation actions. A format action modifies the appearance of a form element so that its contents are shown in a particular way (like a date or time or the number of decimal places). A validation action validates the contents of a form field, ensuring they are always correct. A calculation action is invoked after certain events, like setting a form field value, and lets you establish relationships between form elements and perform calculations.

In addition to processing forms within the document, JavaScript might also play an important role in other areas described in the [Supported Features](#supported-features) section. Disabling all JavaScript in documents that rely on these features could make it unusable. Consider conditionally disabling actions using.

## Example of how to create a JavaScript-enabled PDF document using Adobe Acrobat

## Supported features

Nutrient has basic support for the most common JavaScript API methods and properties, detailed below.

Each of the methods listed below may support only a subset of the features and parameters detailed in the specification. We therefore advise you to test your JavaScript-enabled documents thoroughly. [Contact Support](https://support.nutrient.io/hc/en-us/requests/new) if you run into issues or limitations.

### App

- `alert`, `launchURL`, `viewerVersion`

### Console

- `println`

### Doc

- `getField`, `removeField`, `mailDoc`, `getNthFieldName`, `resetForm`, `print`

- `numFields`, `pageNum`, `info`, `gotoNamedDest`

File attachments aren’t supported when composing emails with `mailDoc`.

### Global

- `global`

### Util

- `printx`, `printd`, `printf`

### Color

- `convert`, `transparent`, `black`, `white`, `red`, `green`, `blue`, `cyan`, `magenta`, `yellow`, `dark gray`, `gray`, `light gray`, custom color arrays

### Event

- `value`, `rc`, `selStart`, `selEnd`, `willCommit`, `target`, `change`, `name`, `type`

### Field

- `getArray`, `checkThisBox`, `isBoxChecked`, `getItemAt`, `setItems`, `clearItems`, `setItems`, `insertItemAt`, `deleteItemAt`, `setAction`, `buttonImportIcon`

- `name`, `value`, `textColor`, `fillColor`, `strokeColor`, `readonly`, `exportValues`, `currentValueIndices`, `multipleSelection`, `commitOnSelChange`, `numItems`, `hidden`, `editable`, `type`, `page`, `borderStyle`, `rotation`, `defaultValue`, `doNotSpellCheck`, `userName`, `alignment`, `rect`, `doc`, `required`, `display`, `calcOrderIndex`, `comb`, `doNotScroll`, `richText`, `multiline`, `fileSelect`, `password`, `charLimit`

Various functions for formatting, validation, and calculation are also supported. You can read Adobe’s documentation to learn more about them.

### Number formatting

- `AFNumber_Format`, `AFNumber_Keystroke`, `AFMakeNumber`

### Percent formatting

- `AFPercent_Format`, `AFPercent_Keystroke`

### Date formatting

- `AFDate_Format`, `AFDate_FormatEx`, `AFDate_Keystroke`, `AFDate_KeystrokeEx`, `AFTime_Format`, `AFTime_FormatEx`, `AFTime_Keystroke`, `AFTime_KeystrokeEx`

### Special formatting

- `AFSpecial_Format`, `AFSpecial_Keystroke`, `AFSpecial_KeystrokeEx`

### Simple formatting

- `AFSimple`, `AFSimple_Calculate`

### Range validation

- `AFRange_Validate`

## Debugging

If there is a problem with the JavaScript in a document, it’s a good idea to first check that the scripts do not contain syntax or logic errors. To see all JavaScript code that is used in a document, open Adobe Acrobat, click on Tools, select Prepare Form, click the small down arrow on the sidebar, and then choose All JavaScripts. The screenshots below shows how the All JavaScripts window appears in Adobe Acrobat.

### JavaScript logs

If JavaScript is enabled, Nutrient will log errors to the console if there’s a problem with the JavaScript code. We tried to make diagnostics as specific as possible to help you debug your documents more easily. For example, the following error message is logged if the script creator mistakenly writes `this.getField(4);`:

```sh

Catalog[58188:7996163] [JavaScript] [Error] Error: Argument name 'cName' has wrong type. Argument has type number, but expected string.

```

As you can read in [the API documentation](https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#getfield), `getField`’s `cName` argument must be a string.

In this other example, the script contains one API property that we currently don’t support:

```sh

Catalog[58188:7996163] [JavaScript] [Error] TypeError: undefined not callable (property 'notSupportedAPI' of [object global])

```

## Learn more

- [JavaScript™ for Acrobat® API Reference](https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/index.html)

- [Developing Acrobat® Applications Using JavaScript™](https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsdevguide/index.html)
---

## Related pages

- [Client authentication and session renewal](/guides/web/viewer/client-authentication.md)
- [Embed Web SDK in a dashboard/app shell](/guides/web/viewer/embed-in-dashboard-app-shell.md)
- [JavaScript image viewer library](/guides/web/viewer/images.md)
- [Create custom annotation toggle button](/guides/web/viewer/custom-annotation-toggle.md)
- [JavaScript PDF viewer library](/guides/web/viewer.md)
- [Page layout and scroll options in our JavaScript PDF viewer](/guides/web/customizing-the-interface/document-presentation-options.md)
- [Office document viewing in JavaScript](/guides/web/viewer/office-documents.md)
- [JavaScript PDF viewer library](/guides/web/viewer/pdf.md)
- [Enhance PDF viewing with linearized downloading](/guides/web/viewer/linearized-downloads.md)
- [Mobile responsive JavaScript PDF viewer](/guides/web/viewer/mobile-responsive.md)
- [Enable or disable permissions in our JavaScript viewer](/guides/web/features/document-permissions.md)
- [Production runtime setup checklist](/guides/web/viewer/production-runtime-setup-checklist.md)
- [PDF document streaming in JavaScript](/guides/web/viewer/streaming.md)
- [Zoom options in our JavaScript PDF viewer](/guides/web/viewer/zooming.md)
- [macOS/Linux](/guides/web/viewer/troubleshooting.md)

