Add PDF Actions in Form Fields Using C#
PDF actions allow you to perform the following tasks:
Actions are triggered by annotations, form fields, or bookmarks. To create an action in a PDF document, use the following steps:
-
Create an annotation as a trigger for the action.
-
Create any action with one of the following methods:
-
Set the action to the annotation.
-
Save the PDF document to a file.
Navigating to a Specified Page
To create an action that navigates to a specified PDF page, use the following steps:
-
Create a
GdPicturePDF
object. -
Load the PDF file with the
LoadFromFile
method. -
Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePDFOrigin
enumeration. -
Set the measurement unit with the
SetMeasurementUnit
method to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnit
enumeration. -
Set the PDF page where you want to place the form field with the
SelectPage
method. -
Add any type of annotation.
-
Create a new action with the
NewActionGoTo
method. It uses the following parameters:-
DestinationType
— A member of thePdfDestinationType
enumeration that specifies the way the destination page is displayed relative to the window. -
Page
— The destination page number. -
Left
— The left coordinate of the document window’s position according to the specifiedDestinationType
parameter. -
Right
— The right coordinate of the document window’s position according to the specifiedDestinationType
parameter. -
Bottom
— The bottom coordinate of the document window’s position according to the specifiedDestinationType
parameter. -
Top
— The top coordinate of the document window’s position according to the specifiedDestinationType
parameter. -
Zoom
— The magnification factor used to display the destination page. To get the 150 percent magnification, use1.5
. To get the current magnification, set this parameter to0
. -
RetainLeft
— Optional: To keep the left coordinate at the current display, set this parameter totrue
. -
RetainRight
— Optional: To keep the right coordinate at the current display, set this parameter totrue
.
-
-
Set the action to the annotation with the
SetAnnotationAction
method. It requires the annotation and the action IDs. -
Save the PDF document to a file with the
SaveToFile
method.
The display of the destination page uses the following rules:
-
The
Left
,Right
,Bottom
,Top
, andZoom
parameters only have an effect when theDestinationType
parameter is set toPdfDestinationType.DestinationTypeXYZ
. -
The
Zoom
parameter is the last parameter executed by the method.
The return value of the
NewActionGoTo
method is the action’s ID. It’s required to assign the trigger element of the newly created action.
To create an action that navigates to the third page of the PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); // Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter); // Select the second PDF page. gdpicturePDF.SelectPage(2); // Create a new action. int actionID = gdpicturePDF.NewActionGoTo(PdfDestinationType.DestinationTypeXYZ, 3, 0, 0, 5, 0, 1.5f); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the second PDF page. gdpicturePDF.SelectPage(2) ' Create a new action. Dim actionID As Integer = gdpicturePDF.NewActionGoTo(PdfDestinationType.DestinationTypeXYZ, 3, 0, 0, 5, 0, 1.5F) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Navigating to a Specified Page of a Different PDF
To create an action that navigates to a specified page of a different PDF document, use the following steps:
-
Create a
GdPicturePDF
object. -
Load the PDF file with the
LoadFromFile
method. -
Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePDFOrigin
enumeration. -
Set the measurement unit with the
SetMeasurementUnit
method to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnit
enumeration. -
Set the PDF page where you want to place the form field with the
SelectPage
method. -
Add any type of annotation.
-
Create a new action with the
NewActionGoToR
method. It uses the following parameters:-
DestinationType
— A member of thePdfDestinationType
enumeration that specifies the way the destination page is displayed relative to the window. -
FilePath
— The relative path to the destination file. -
NewWindow
— A Boolean value that specifies whether to open the destination PDF document in a new window. -
Page
— The destination page number. -
Left
— The left coordinate of the document window’s position according to the specifiedDestinationType
parameter. -
Right
— The right coordinate of the document window’s position according to the specifiedDestinationType
parameter. -
Bottom
— The bottom coordinate of the document window’s position according to the specifiedDestinationType
parameter. -
Top
— The top coordinate of the document window’s position according to the specifiedDestinationType
parameter. -
Zoom
— The magnification factor used to display the destination page. To get the 150 percent magnification, use1.5
. To get the current magnification, set this parameter to0
. -
RetainLeft
— Optional: To keep the left coordinate at the current display, set this parameter totrue
. -
RetainRight
— Optional: To keep the right coordinate at the current display, set this parameter totrue
.
-
-
Set the action to the annotation with the
SetAnnotationAction
method. It requires the annotation and the action IDs. -
Save the PDF document to a file with the
SaveToFile
method.
The display of the destination page uses the following rules:
-
The
Left
,Right
,Bottom
,Top
, andZoom
parameters only have an effect when theDestinationType
parameter is set toPdfDestinationType.DestinationTypeXYZ
. -
The
Zoom
parameter is the last parameter executed by the method.
The return value of the
NewActionGoToR
method is the action’s ID. It’s required to assign the trigger element of the newly created action.
To create an action that navigates to the third page of the PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); // Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter); // Select the second PDF page. gdpicturePDF.SelectPage(2); // Create a new action. int actionID = gdpicturePDF.NewActionGoToR(PdfDestinationType.DestinationTypeXYZ, @"C:\temp\destination.pdf", false, 3, 0, 0, 5, 0, 1.5f); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the second PDF page. gdpicturePDF.SelectPage(2) ' Create a new action. Dim actionID As Integer = gdpicturePDF.NewActionGoToR(PdfDestinationType.DestinationTypeXYZ, @"C:\temp\destination.pdf", false, 3, 0, 0, 5, 0, 1.5f) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Launching an Application
To create an action that launches an application, use the following steps:
-
Create a
GdPicturePDF
object. -
Load the PDF file with the
LoadFromFile
method. -
Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePDFOrigin
enumeration. -
Set the measurement unit with the
SetMeasurementUnit
method to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnit
enumeration. -
Set the PDF page where you want to place the form field with the
SelectPage
method. -
Add any type of annotation.
-
Create a new action with the
NewActionLaunch
method. It uses the following parameters:-
FileName
— The application file name. -
DefaultDirectory
— Specifies the default directory in a standard DOS syntax. It’s a Windows-specific parameter. If you aren’t sure what value to assign, use an empty string. -
Parameters
— A parameter string passed to the application. It’s a Windows-specific parameter. If you aren’t sure what value to assign, use an empty string. -
Operation
— A member of thePdfActionLaunchOperation
enumeration. It’s a Windows-specific parameter. If you aren’t sure what value to assign, set it toPdfActionLaunchOperation.ActionLaunchOperationUndefined
. -
NewWindow
— A Boolean value that specifies whether to open the destination PDF document in a new window.
-
-
Set the action to the annotation with the
SetAnnotationAction
method. It requires the annotation and the action IDs. -
Save the PDF document to a file with the
SaveToFile
method.
The return value of the
NewActionLaunch
method is the action’s ID. It’s required to assign the trigger element of the newly created action.
To create an action that navigates to the third page of the PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); // Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter); // Select the second PDF page. gdpicturePDF.SelectPage(2); // Create a new action. int actionID = gdpicturePDF.NewActionLaunch("notepad.exe", "", "", PdfActionLaunchOperation.ActionLaunchOperationUndefined, true); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the second PDF page. gdpicturePDF.SelectPage(2) ' Create a new action. Dim actionID As Integer = gdpicturePDF.NewActionLaunch("notepad.exe", "", "", PdfActionLaunchOperation.ActionLaunchOperationUndefined, true) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Opening a Link to a Website
To create an action that opens a website, use the following steps:
-
Create a
GdPicturePDF
object. -
Load the PDF file with the
LoadFromFile
method. -
Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePDFOrigin
enumeration. -
Set the measurement unit with the
SetMeasurementUnit
method to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnit
enumeration. -
Set the PDF page where you want to place the form field with the
SelectPage
method. -
Add any type of annotation.
-
Create a new action with the
NewActionLaunch
method. It uses the following parameters:-
URI
— Website address represented as a uniform resource identifier and encoded in 7-bit ASCII. -
IsMap
— Specifies whether to track the mouse position when the URI is resolved. This behavior works only for actions triggered from a PDF annotation.
-
-
Set the action to the annotation with the
SetAnnotationAction
method. It requires the annotation and the action IDs. -
Save the PDF document to a file with the
SaveToFile
method.
The return value of the
NewActionURI
method is the action’s ID. It’s required to assign the trigger element of the newly created action.
To create an action that navigates to the third page of the PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); // Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter); // Select the second PDF page. gdpicturePDF.SelectPage(2); // Create a new action. int actionID = gdpicturePDF.NewActionURI(@"https:\\pspdfkit.com\", false); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the second PDF page. gdpicturePDF.SelectPage(2) ' Create a new action. Dim actionID As Integer = gdpicturePDF.NewActionURI(@"https:\\pspdfkit.com\", false) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Running JavaScript
To create an action that triggers JavaScript, use the following steps:
-
Create a
GdPicturePDF
object. -
Load the PDF file with the
LoadFromFile
method. -
Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePDFOrigin
enumeration. -
Set the measurement unit with the
SetMeasurementUnit
method to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnit
enumeration. -
Set the PDF page where you want to place the form field with the
SelectPage
method. -
Add any type of annotation.
-
Create a new action with the
NewActionJavaScript
method. It uses a string parameter as its parameter, which contains the JavaScript code. -
Set the action to the annotation with the
SetAnnotationAction
method. It requires the annotation and the action IDs. -
Save the PDF document to a file with the
SaveToFile
method.
The return value of the
NewActionURI
method is the action’s ID. It’s required to assign the trigger element of the newly created action.
To create an action that navigates to the third page of the PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); // Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter); // Select the second PDF page. gdpicturePDF.SelectPage(2); // Create a new action. int actionID = gdpicturePDF.NewActionJavaScript("app.alert(\"Hello!\");"); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the second PDF page. gdpicturePDF.SelectPage(2) ' Create a new action. Dim actionID As Integer = gdpicturePDF.NewActionJavaScript("app.alert(\"Hello!\");") gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
Getting the Action ID
To get the ID of an action associated with an annotation, use the GetAnnotationActionID
method. It requires the annotation ID as its parameter:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); // Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter); // Select the second PDF page. gdpicturePDF.SelectPage(2); string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica); // Add a button form field. int formID = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue); // Create a new action. gdpicturePDF.NewActionURI(@"https:\\pspdfkit.com\", false); // Save the action ID to a variable. int actionID = gdpicturePDF.GetAnnotationActionID(formID); // Set the action to the form field gdpicturePDF.SetFormFieldAction(formID, actionID); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the second PDF page. gdpicturePDF.SelectPage(2) Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica) ' Add a button form field. Dim formID As Integer = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue) ' Create a new action. gdpicturePDF.NewActionURI("https:\\pspdfkit.com\", False) ' Save the action ID to a variable. Dim actionID As Integer = gdpicturePDF.GetAnnotationActionID(formID) ' Set the action to the form field gdpicturePDF.SetFormFieldAction(formID, actionID) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Getting the Action Properties
To get the action’s properties, use any of the following methods:
-
GetActionType
— Returns a member of thePdfActionType
enumeration that specifies the action type. -
GetActionPageDestination
— Returns all parameters of an action that navigates to a different page of the currently opened PDF document. -
GetActionRemotePageDestination
— Returns all parameters of an action that navigates to a different page of a different PDF document. -
GetActionLaunchDestination
— Returns all parameters of an action that opens a new application. -
GetActionURI
— Returns the URL of the destination website. -
GetActionJavaScript
— Returns the JavaScript code triggered by the action.