PDF Actions

A PDF action is similar to a web hyperlink, but it’s much more flexible. PSPDFKit for Web implements common actions as defined in Adobe’s PDF Reference (page 417ff).

Action PSPDFKit Class Use Case
GoTo GoToAction Go to a destination (page) in the current document.
URI URIAction Resolve a Uniform Resource Identifier (web link).
SubmitForm SubmitFormAction Send data to a Uniform Resource Locator.
ResetForm ResetFormAction Set fields to their default values.
JavaScript JavaScriptAction Execute a script.

GoToAction

A GoTo action can define a different pageIndex in the same document. Clicking on it updates the scroll position to make the page visible, but it doesn’t update the zoom level.

URIAction

A URI action contains a URI. When executing this annotation, PSPDFKit uses window.open to create a new browser tab, which also clears the opener as a security measurement to avoid allowing the target page to have access to your PDF state:

1
2
let newWindow = window.open(action.uri, "_blank");
newWindow.opener = null;
1
2
var newWindow = window.open(action.uri, "_blank");
newWindow.opener = null;

JavaScriptAction

This is a PDF action for running arbitrary JavaScript. Actions are executed on a click. WidgetAnnotation and form fields can also define an additionalActions field, which is a dictionary of event name and action pairs. For more information about the additional actions of WidgetAnnotations and FormFields, refer to the API documentation.

You can learn more about the security problems when using _blank in this article from JitBit.

Please refer to an individual browser’s documentation for a list of supported URI protocols. The most used protocols (http, https, and mailto) are supported in all major browsers.

Here is an example showing how to run JavaScript code when a WidgetAnnotation is focused. This is done using the additionalActions field:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const widget = new PSPDFKit.Annotations.WidgetAnnotation({
  pageIndex: 0,
  formFieldName: "MyFormField",
  boundingBox: new PSPDFKit.Geometry.Rect({
    left: 100,
    top: 75,
    width: 200,
    height: 80
  }),
  additionalActions: {
    onFocus: new PSPDFKit.Actions.JavaScriptAction({
      script: "alert('onFocus')"
    })
  }
});

instance.createAnnotation(widget).then(annotation => {
  instance.createFormField(
    new PSPDFKit.FormFields.TextFormField({
      name: "MyFormField",
      annotationIds: new PSPDFKit.Immutable.List([annotation.id]),
      value: "Text shown in the form field"
    })
  );
});