Add PDF actions using C# in form fields
PDF actions enable 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
GdPicturePDFobject. - Load the PDF file with the
LoadFromFilemethod. - Set the origin of the coordinate system with the
SetOriginmethod. This method requires thePDFOriginenumeration. - Set the measurement unit with the
SetMeasurementUnitmethod to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the form field with the
SelectPagemethod. - Add any type of annotation.
- Create a new action with the
NewActionGoTomethod. It uses the following parameters:DestinationType— A member of thePdfDestinationTypeenumeration 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 specifiedDestinationTypeparameter.Right— The right coordinate of the document window’s position according to the specifiedDestinationTypeparameter.Bottom— The bottom coordinate of the document window’s position according to the specifiedDestinationTypeparameter.Top— The top coordinate of the document window’s position according to the specifiedDestinationTypeparameter.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
SetAnnotationActionmethod. It requires the annotation and the action IDs. - Save the PDF document to a file with the
SaveToFilemethod.
The display of the destination page uses the following rules:
- The
Left,Right,Bottom,Top, andZoomparameters only have an effect when theDestinationTypeparameter is set toPdfDestinationType.DestinationTypeXYZ. - The
Zoomparameter 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 UsingNavigating 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
GdPicturePDFobject. - Load the PDF file with the
LoadFromFilemethod. - Set the origin of the coordinate system with the
SetOriginmethod. This method requires thePDFOriginenumeration. - Set the measurement unit with the
SetMeasurementUnitmethod to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the form field with the
SelectPagemethod. - Add any type of annotation.
- Create a new action with the
NewActionGoToRmethod. It uses the following parameters:DestinationType— A member of thePdfDestinationTypeenumeration 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 specifiedDestinationTypeparameter.Right— The right coordinate of the document window’s position according to the specifiedDestinationTypeparameter.Bottom— The bottom coordinate of the document window’s position according to the specifiedDestinationTypeparameter.Top— The top coordinate of the document window’s position according to the specifiedDestinationTypeparameter.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
SetAnnotationActionmethod. It requires the annotation and the action IDs. - Save the PDF document to a file with the
SaveToFilemethod.
The display of the destination page uses the following rules:
- The
Left,Right,Bottom,Top, andZoomparameters only have an effect when theDestinationTypeparameter is set toPdfDestinationType.DestinationTypeXYZ. - The
Zoomparameter 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 UsingLaunching an application
To create an action that launches an application, use the following steps:
- Create a
GdPicturePDFobject. - Load the PDF file with the
LoadFromFilemethod. - Set the origin of the coordinate system with the
SetOriginmethod. This method requires thePDFOriginenumeration. - Set the measurement unit with the
SetMeasurementUnitmethod to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the form field with the
SelectPagemethod. - Add any type of annotation.
- Create a new action with the
NewActionLaunchmethod. 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 thePdfActionLaunchOperationenumeration. 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
SetAnnotationActionmethod. It requires the annotation and the action IDs. - Save the PDF document to a file with the
SaveToFilemethod.
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 UsingOpening a link to a website
To create an action that opens a website, use the following steps:
- Create a
GdPicturePDFobject. - Load the PDF file with the
LoadFromFilemethod. - Set the origin of the coordinate system with the
SetOriginmethod. This method requires thePDFOriginenumeration. - Set the measurement unit with the
SetMeasurementUnitmethod to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the form field with the
SelectPagemethod. - Add any type of annotation.
- Create a new action with the
NewActionLaunchmethod. 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
SetAnnotationActionmethod. It requires the annotation and the action IDs. - Save the PDF document to a file with the
SaveToFilemethod.
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 UsingRunning JavaScript
To create an action that triggers JavaScript, use the following steps:
- Create a
GdPicturePDFobject. - Load the PDF file with the
LoadFromFilemethod. - Set the origin of the coordinate system with the
SetOriginmethod. This method requires thePDFOriginenumeration. - Set the measurement unit with the
SetMeasurementUnitmethod to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the form field with the
SelectPagemethod. - Add any type of annotation.
- Create a new action with the
NewActionJavaScriptmethod. It uses a string parameter as its parameter, which contains the JavaScript code. - Set the action to the annotation with the
SetAnnotationActionmethod. It requires the annotation and the action IDs. - Save the PDF document to a file with the
SaveToFilemethod.
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 UsingGetting 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 fieldgdpicturePDF.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 UsingGetting the action properties
To get the action’s properties, use any of the following methods:
GetActionType— Returns a member of thePdfActionTypeenumeration 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.