PDF: Managing actions of PDF document
In This Topic
A PDF document allows to define actions, which should be performed by application while PDF document is being viewed.
The actions may be simple, e.g. go to a destination in the current document, or complex, e.g. launch an external application
or JavaScript code.
Supported action types
PdfAction class is the base class for any action of PDF document.
Here is a list of actions supported by the SDK:
An action can be composite. To create the composite action is used
PdfAction.NextActions property.
Each action must be associated with an element of PDF document. The action can be associated with a document,
page, bookmark, annotation, interactive form etc.
Executors of actions
The executor of action contains a logic of performing an action of certain type. The base class for executors of actions is
PdfActionExecutorBase class.
Here is a list of executors of actions supported by the SDK:
Here is a list of executors of actions implemented in demo applications (PdfDemosCommonCode):
- PdfViewerNamedActionExecutor - performs execution of PdfViewerNamedAction, i.e. navigates in image viewer
- PdfUriActionExecutor - performs execution of PdfUriAction, i.e. opens hypertext link in default browser
- PdfLaunchActionExecutor - performs execution of PdfLaunchAction, i.e. launches an application
- PdfSubmitActionExecutor - performs execution of PdfSubmitFormAction, i.e. transmits the names and values of selected interactive form fields to a specified URL
Here is an example that demonstrates how to configure the execution of all supported actions (incl. the execution of JavaScript actions) for the visual tool
PdfAnnotationTool:
/// <summary>
/// Creates the PDF annotation tool with JavaScript support.
/// </summary>
/// <param name="viewer">The image viewer.</param>
public static Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool CreatePdfAnnotationToolWithJavaScriptSupport(Vintasoft.Imaging.UI.ImageViewer viewer)
{
// create PDF JavaScript application
Vintasoft.Imaging.Pdf.UI.JavaScript.WinFormsPdfJsApp jsApp =
new Vintasoft.Imaging.Pdf.UI.JavaScript.WinFormsPdfJsApp();
// add PDF documents, which are associated with images in viewer,
// to the document set of PDF JavaScript application
jsApp.RegisterImageViewer(viewer);
// create PdfJavaScriptActionExecutor for PDF JavaScript application
jsApp.ActionExecutor = new Vintasoft.Imaging.Pdf.JavaScript.PdfJavaScriptActionExecutor(
jsApp, new Vintasoft.Imaging.Pdf.JavaScriptApi.PdfJsConsole());
// create Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool
Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool annotationTool =
new Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool(jsApp, true);
annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Markup;
// create an application action executor
Vintasoft.Imaging.Pdf.PdfActionCompositeExecutor applicationActionExecutor =
new Vintasoft.Imaging.Pdf.PdfActionCompositeExecutor();
// add executor for PdfJavaScriptAction to the application action executor
applicationActionExecutor.Items.Add(jsApp.ActionExecutor);
// add executor for PdfGotoAction to the application action executor
applicationActionExecutor.Items.Add(new Vintasoft.Imaging.Pdf.UI.PdfGotoActionExecutor(viewer));
// add executor for PdfNamedAction to the application action executor
applicationActionExecutor.Items.Add(new Vintasoft.Imaging.Pdf.UI.PdfNamedActionExecutor(viewer));
// add executor for PdfResetFormAction to the application action executor
applicationActionExecutor.Items.Add(
new Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationToolResetFormActionExecutor(annotationTool));
// add executor for PdfAnnotationHideAction to the application action executor
applicationActionExecutor.Items.Add(
new Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationToolAnnotationHideActionExecutor(annotationTool));
// add executor for PdfUriAction to the application action executor,
// executor is located in WpfPdfDemosCommonCode
//applicationActionExecutor.Items.Add(new PdfUriActionExecutor());
// add executor for PdfLaunchAction to the application action executor,
// executor is located in WpfPdfDemosCommonCode
//applicationActionExecutor.Items.Add(new PdfLaunchActionExecutor());
// add executor for PdfSubmitFormAction to the application action executor,
// executor is located in WpfPdfDemosCommonCode
//applicationActionExecutor.Items.Add(new PdfSubmitActionExecutor(viewer));
// add the default executor (for PdfResetFormAction and PdfAnnotationHideAction)
// to the application action executor
applicationActionExecutor.Items.Add(new Vintasoft.Imaging.Pdf.PdfActionExecutor());
// set the application action executor as action executor of PDF annotation tool
annotationTool.ActionExecutor = applicationActionExecutor;
// create a document-level actions executor
Vintasoft.Imaging.Pdf.PdfDocumentLevelActionsExecutor documentLevelActionsExecutor =
new Vintasoft.Imaging.Pdf.PdfDocumentLevelActionsExecutor(jsApp);
// set the application action executor as action executor of the document-level actions executor
documentLevelActionsExecutor.ActionExecutor = applicationActionExecutor;
return annotationTool;
}
''' <summary>
''' Creates the PDF annotation tool with JavaScript support.
''' </summary>
''' <param name="viewer">The image viewer.</param>
Public Shared Function CreatePdfAnnotationToolWithJavaScriptSupport(viewer As Vintasoft.Imaging.UI.ImageViewer) As Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool
' create PDF JavaScript application
Dim jsApp As New Vintasoft.Imaging.Pdf.UI.JavaScript.WinFormsPdfJsApp()
' add PDF documents, which are associated with images in viewer,
' to the document set of PDF JavaScript application
jsApp.RegisterImageViewer(viewer)
' create PdfJavaScriptActionExecutor for PDF JavaScript application
jsApp.ActionExecutor = New Vintasoft.Imaging.Pdf.JavaScript.PdfJavaScriptActionExecutor(jsApp, New Vintasoft.Imaging.Pdf.JavaScriptApi.PdfJsConsole())
' create Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool
Dim annotationTool As New Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool(jsApp, True)
annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Markup
' create an application action executor
Dim applicationActionExecutor As New Vintasoft.Imaging.Pdf.PdfActionCompositeExecutor()
' add executor for PdfJavaScriptAction to the application action executor
applicationActionExecutor.Items.Add(jsApp.ActionExecutor)
' add executor for PdfGotoAction to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.UI.PdfGotoActionExecutor(viewer))
' add executor for PdfNamedAction to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.UI.PdfNamedActionExecutor(viewer))
' add executor for PdfResetFormAction to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationToolResetFormActionExecutor(annotationTool))
' add executor for PdfAnnotationHideAction to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationToolAnnotationHideActionExecutor(annotationTool))
' add executor for PdfUriAction to the application action executor,
' executor is located in WpfPdfDemosCommonCode
'applicationActionExecutor.Items.Add(new PdfUriActionExecutor());
' add executor for PdfLaunchAction to the application action executor,
' executor is located in WpfPdfDemosCommonCode
'applicationActionExecutor.Items.Add(new PdfLaunchActionExecutor());
' add executor for PdfSubmitFormAction to the application action executor,
' executor is located in WpfPdfDemosCommonCode
'applicationActionExecutor.Items.Add(new PdfSubmitActionExecutor(viewer));
' add the default executor (for PdfResetFormAction and PdfAnnotationHideAction)
' to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.PdfActionExecutor())
' set the application action executor as action executor of PDF annotation tool
annotationTool.ActionExecutor = applicationActionExecutor
' create a document-level actions executor
Dim documentLevelActionsExecutor As New Vintasoft.Imaging.Pdf.PdfDocumentLevelActionsExecutor(jsApp)
' set the application action executor as action executor of the document-level actions executor
documentLevelActionsExecutor.ActionExecutor = applicationActionExecutor
Return annotationTool
End Function
Actions of document
An action that have to be performed when opening a PDF document can be defined using
PdfDocument.OpenAction property.
The
PdfDocument.AdditionalActions property allows to define additional actions for PDF document,
notably the actions which have to be performed before the document is saved or printed,
and the actions which have to be performed after the document is saved, printed or closed.
Actions of page
The
PdfPage.AdditionalActions property allows to define additional actions for PDF page, notably the actions
which have to be performed when the page is opened or closed.
Actions of bookmark
The
PdfBookmark.Action property allows to define an action which have to be performed when the bookmark is becoming active.
Here is an example, that demonstrates how to change an action which have to be performed when a bookmark becomes active:
/// <summary>
/// Changes action of the first bookmark of PDF document.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
public static void ChangeFirstBookmarkAction(string pdfFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
// create goto action to the last page of PDF document
Vintasoft.Imaging.Pdf.Tree.PdfDestinationFit dest =
new Vintasoft.Imaging.Pdf.Tree.PdfDestinationFit(document, document.Pages[document.Pages.Count - 1]);
Vintasoft.Imaging.Pdf.Tree.PdfGotoAction newAction =
new Vintasoft.Imaging.Pdf.Tree.PdfGotoAction(dest);
// get the first bookmark of PDF document
Vintasoft.Imaging.Pdf.Tree.PdfBookmark bookmark = document.Bookmarks[0];
// change the action of bookmark
bookmark.Action = newAction;
// save changes to a file
document.SaveChanges();
}
}
''' <summary>
''' Changes action of the first bookmark of PDF document.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
Public Shared Sub ChangeFirstBookmarkAction(pdfFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
' create goto action to the last page of PDF document
Dim dest As New Vintasoft.Imaging.Pdf.Tree.PdfDestinationFit(document, document.Pages(document.Pages.Count - 1))
Dim newAction As New Vintasoft.Imaging.Pdf.Tree.PdfGotoAction(dest)
' get the first bookmark of PDF document
Dim bookmark As Vintasoft.Imaging.Pdf.Tree.PdfBookmark = document.Bookmarks(0)
' change the action of bookmark
bookmark.Action = newAction
' save changes to a file
document.SaveChanges()
End Using
End Sub
Actions of annotation
PdfAnnotation class is a base class for any annotation of PDF document.
PdfAnnotation.ActivateAction property allows to define an action, which have to be performed when annotation is activated.
PdfAnnotation.AdditionalActions property allows to define additional actions, e.g. an action which have to be performed
when an annotation is clicked on by a mouse button or an action which have to be performed when a page with annotation
has become visible.
Actions of interactive form field
PdfInteractiveFormField class is a base class for any field of interactive form of PDF document.
PdfInteractiveFormField.AdditionalActions property allows to define actions connected with a form field.