PDF Annotation Actions on iOS

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 Action subclass can either be parsed from a PDF or created in code. It can also be serialized via NSKeyedArchiver (supporting NSSecureCoding):

Action PSPDFKit Class Use Case
GoTo GoToAction Go to a destination (page) in the current document.
GoToR RemoteGoToAction Go to a destination in another document.
GoToE EmbeddedGoToAction Go to a destination in an embedded file.
Launch RemoteGoToAction Launch an application, usually to open a file.
URI URLAction Resolve a uniform resource identifier (web link).
Hide HideAction Set an annotation’s hidden flag.
JavaScript JavaScriptAction Execute a JavaScript script.
Rendition RenditionAction Control the playing of multimedia content.
SubmitForm SubmitFormAction Send data to a uniform resource locator.
ResetForm ResetFormAction Set fields to their default values.
Named NamedAction 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 LinkAnnotation and WidgetAnnotation, 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 Annotation.TriggerEvent.mouseUp event is what you want.

Bookmark and OutlineElement 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 Annotation.TriggerEvent for available trigger events. PSPDFKit supports a subset of these events.

Executing Actions

You can use PDFViewController’s execute(_:targetRect:pageIndex:animated:actionContainer:) to execute PDF actions, such as the action property of LinkAnnotation:

pdfController.execute(linkAnnotation.action, targetRect: linkAnnotation.boundingBox, pageIndex: linkAnnotation.absolutePageIndex, animated: true, actionContainer: linkAnnotation)
[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, and the files in question should be in a local folder.