When modifying PDF form fields in Nutrient Web SDK, you may encounter errors if the fields contain unsupported JavaScript actions. This guide explains how to identify and resolve such errors.

The problem

Attempting to modify form fields that contain unsupported JavaScript actions, such as onBlur, can result in the following errors:

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'script')
Uncaught (in promise) PSPDFKitError: Assertion failed: JavaScriptAction requires script

Understanding action support

Form field objects and widget annotations support different sets of actions:

Solution

To modify form fields with unsupported actions, first remove these actions, and then apply your desired changes:

NutrientViewer.load({
...baseOptions,
theme: NutrientViewer.Theme.DARK,
}).then(async (instance) => {
const formFields = await instance.getFormFields();
// Identify form fields with onBlur actions and remove only that action.
const fieldsWithOnBlur = formFields
.filter((formField) => {
return formField?.additionalActions?.onBlur;
})
.map((formField) => formField.set("additionalActions", formField.additionalActions.delete("onBlur")));
try {
// Remove the onBlur actions.
await instance.update(fieldsWithOnBlur);
// Apply your desired changes (for example, setting fields to read-only).
await instance.update(
(await instance.getFormFields()).map((formField) =>
formField.set("readOnly", true)
)
);
} catch (error) {
console.log(error);
}
});

Try this in the playground

This approach:

  1. Identifies form fields with onBlur actions.
  2. Surgically removes only the onBlur action while preserving other supported actions.
  3. Applies the desired changes to the form fields.
  4. Uses a try/catch block to handle any remaining errors.

Important considerations

  • Form field objects don't support the onBlur action, but widget annotations do.
  • For PDFs with many form fields or complex embedded JavaScript, some errors may still occur when rendering new pages. If this happens, contact our support team(opens in a new tab) with the specific error messages and a sample PDF if possible.
  • The try/catch block ensures your code continues to run even if some errors occur.

For more information about JavaScript actions support in Nutrient Web SDK, refer to the PDF actions support guide.