Actions

edit

Set actions

An action is what a viewer does on an event: go to a page, open a URI, run a script, reset a form... The static methods of PdfAction create them, and the objects that have actions take them:

// Open the document on its last page, and run a script before it is printed
document.OpenAction = PdfAction.GoTo(PdfDestination.Fit(last));
document.AdditionalActions[PdfActionTrigger.DocumentPrint] = PdfAction.JavaScript("app.alert('Printing')");

// A link that goes to the next page, then opens a website
var link = page.Annotations.AddLink(new PdfRectangle(72, 700, 200, 720), new Uri("https://example.com"));
var next = PdfAction.Named("NextPage");
next.SetNext(PdfAction.OpenUri(new Uri("https://www.meziantou.net")));
link.Action = next;

// A bookmark that resets the form
document.Outlines.Add("Reset").Action = PdfAction.ResetForm();
MethodAction
GoToGo to a destination of the document
GoToRemoteOpen a page of another PDF file
LaunchOpen a file with the application of the system
OpenUriOpen a URI
NamedA command of the viewer: NextPage, PrevPage, FirstPage, LastPage
SubmitForm, ResetForm, ImportDataSend, reset or import the values of the form fields
HideHide or show form fields
SetOptionalContentStateTurn layers on or off: see Layers
JavaScriptRun a script

SetNext chains actions: they run in order.

Where actions go

ObjectProperty
The document, when it is openedPdfDocument.OpenAction
The document, before it is closed, saved or printedPdfDocument.AdditionalActions
A page, when it is opened or closedPdfPage.AdditionalActions
A linkPdfLinkAnnotation.Action
A bookmarkPdfOutlineItem.Action
A widget, such as a push buttonPdfWidgetAnnotation.Action
An annotation, when the mouse enters it, it gets the focus...PdfAnnotation.AdditionalActions
  • The additional actions are indexed by PdfActionTrigger; SupportedTriggers lists the events of the object, and Triggers those that have an action. Setting null removes an action.
  • A link or a bookmark has a destination or an action: setting one removes the other. The destination of a link, or of the open action, is read as a PdfGoToAction.
  • An action that refers to the pages or the layers of another document is refused.

Read the actions of a file

Each action is an instance of the class of its type: PdfGoToAction, PdfGoToRemoteAction, PdfGoToEmbeddedAction, PdfLaunchAction, PdfUriAction, PdfNamedAction, PdfSubmitFormAction, PdfResetFormAction, PdfImportDataAction, PdfHideAction, PdfSetOptionalContentStateAction and PdfJavaScriptAction. The threads, sounds, movies, renditions, transitions, 3D views and rich media are PdfUnknownAction: read their Dictionary.

Next reads the actions that follow an action. The sequence is bounded: an action reached twice ends it. To run the scripts of the form fields, see Form scripts.