PDF Actions

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

The most common types are GoTo and URI. However, many documents also use GoToR, Launch, Named, or JavaScript for page changes.

Every PSPDFAction subclass can either be parsed from a PDF or created in code. It can also be serialized via NSKeyedArchiver (supporting NSSecureCoding) or serialized/deserialized via PSPDFJSONAdapter:

Action PSPDFKit Class Use Case
GoTo PSPDFGoToAction Go to a destination (page) in the current document.
GoToR PSPDFRemoteGoToAction Go to a destination in another document.
GoToE PSPDFEmbeddedGoToAction Go to a destination in an embedded file.
Launch PSPDFRemoteGoToAction Launch an application, usually to open a file.
URI PSPDFURLAction Resolve a uniform resource identifier (web link).
Hide PSPDFHideAction Set an annotation’s hidden flag.
JavaScript PSPDFJavaScriptAction Execute a JavaScript script.
Rendition PSPDFRenditionAction Control the playing of multimedia content.
SubmitForm PSPDFSubmitFormAction Send data to a uniform resource locator.
ResetForm PSPDFResetFormAction Set fields to their default values.
Named PSPDFNamedAction Execute an action predefined by the conforming reader.

Unsupported actions include Thread, Sound, Movie, ImportData, SetOCGState, Trans, and GoTo3DView.

Actions are chainable via the subActions property. Certain annotations, such as the PSPDFLinkAnnotation and the PSPDFWidgetAnnotation, can contain an action. Furthermore, every annotation can contain additionalActions that are executed on certain trigger events. You can use the additionalActions property to add actions to annotation types that don’t have an action handler by default, such as stamp annotations. In most cases, the PSPDFAnnotationTriggerEventMouseUp event is what you want.

PSPDFBookmark and PSPDFOutlineElement also contain an action, thereby supporting the same types of actions as an annotation. (A PDF outline can open a web link or even play a video.)

Trigger Events

See PSPDFAnnotationTriggerEvent for available trigger events. PSPDFKit supports a subset of these events.

Executing Actions

You can use PSPDFViewController’s -executePDFAction:targetRect:pageIndex:animated:actionContainer: to execute PDF actions, such as the action property of PSPDFLinkAnnotation:

Copy
1
pdfController.execute(linkAnnotation.action, targetRect: linkAnnotation.boundingBox, pageIndex: linkAnnotation.absolutePageIndex, animated: true, actionContainer: linkAnnotation)
Copy
1
[pdfController executePDFAction:linkAnnotation.action targetRect:linkAnnotation.boundingBox pageIndex:linkAnnotation.absolutePageIndex animated:YES actionContainer:linkAnnotation];

Note: The file structure for the action has to be correctly specified for it to work. The files in question should be in a local folder.