Actions
editSet 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();| Method | Action |
|---|---|
| GoTo | Go to a destination of the document |
| GoToRemote | Open a page of another PDF file |
| Launch | Open a file with the application of the system |
| OpenUri | Open a URI |
| Named | A command of the viewer: NextPage, PrevPage, FirstPage, LastPage |
| SubmitForm, ResetForm, ImportData | Send, reset or import the values of the form fields |
| Hide | Hide or show form fields |
| SetOptionalContentState | Turn layers on or off: see Layers |
| JavaScript | Run a script |
SetNext chains actions: they run in order.
Where actions go
| Object | Property |
|---|---|
| The document, when it is opened | PdfDocument.OpenAction |
| The document, before it is closed, saved or printed | PdfDocument.AdditionalActions |
| A page, when it is opened or closed | PdfPage.AdditionalActions |
| A link | PdfLinkAnnotation.Action |
| A bookmark | PdfOutlineItem.Action |
| A widget, such as a push button | PdfWidgetAnnotation.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
nullremoves 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.