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:

  1. Create a form field as a trigger for the action.

  2. Create any action with one of the following methods:

  3. Set the action to the form field.

  4. Save the PDF document to a file.

To create an action that navigates to a specified PDF page, use the following steps:

  1. Create a GdPicturePDF object.

  2. Load the PDF file with the LoadFromFile method.

  3. Set the origin of the coordinate system with the SetOrigin method. This method requires the PDFOrigin enumeration.

  4. Set the measurement unit with the SetMeasurementUnit method to specify the form field’s dimensions and position. This method uses the PdfMeasurementUnit enumeration.

  5. Set the PDF page where you want to place the form field with the SelectPage method.

  6. Add any type of form field.

  7. Create a new action with the NewActionGoTo method. It uses the following parameters:

    • DestinationType — A member of the PdfDestinationType 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 specified DestinationType parameter.

    • Right — The right coordinate of the document window’s position according to the specified DestinationType parameter.

    • Bottom — The bottom coordinate of the document window’s position according to the specified DestinationType parameter.

    • Top — The top coordinate of the document window’s position according to the specified DestinationType parameter.

    • Zoom — The magnification factor used to display the destination page. To get the 150 percent magnification, use 1.5. To get the current magnification, set this parameter to 0.

    • RetainLeft — Optional: To keep the left coordinate at the current display, set this parameter to true.

    • RetainRight — Optional: To keep the right coordinate at the current display, set this parameter to true.

  8. Set the action to the form field with the SetFormFieldAction method. It requires the form field and the action IDs.

  9. 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, and Zoom parameters only have an effect when the DestinationType parameter is set to PdfDestinationType.DestinationTypeXYZ.

  • The Zoom parameter is the last parameter executed by the method.

Information

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);
string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);
// Add a form field.
int formID = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue);
// 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)
    ' Create a new action.
    Dim actionID As Integer = gdpicturePDF.NewActionGoTo(PdfDestinationType.DestinationTypeXYZ, 3, 0, 0, 5, 0, 1.5F)
    Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
    ' Add a form field.
    Dim formID As Integer = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue)
    ' Set the action to the form field
    gdpicturePDF.SetFormFieldAction(formID, actionID)
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics

To create an action that navigates to a specified page of a different PDF document, use the following steps:

  1. Create a GdPicturePDF object.

  2. Load the PDF file with the LoadFromFile method.

  3. Set the origin of the coordinate system with the SetOrigin method. This method requires the PDFOrigin enumeration.

  4. Set the measurement unit with the SetMeasurementUnit method to specify the form field’s dimensions and position. This method uses the PdfMeasurementUnit enumeration.

  5. Set the PDF page where you want to place the form field with the SelectPage method.

  6. Add any type of form field.

  7. Create a new action with the NewActionGoToR method. It uses the following parameters:

    • DestinationType — A member of the PdfDestinationType 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 specified DestinationType parameter.

    • Right — The right coordinate of the document window’s position according to the specified DestinationType parameter.

    • Bottom — The bottom coordinate of the document window’s position according to the specified DestinationType parameter.

    • Top — The top coordinate of the document window’s position according to the specified DestinationType parameter.

    • Zoom — The magnification factor used to display the destination page. To get the 150 percent magnification, use 1.5. To get the current magnification, set this parameter to 0.

    • RetainLeft — Optional: To keep the left coordinate at the current display, set this parameter to true.

    • RetainRight — Optional: To keep the right coordinate at the current display, set this parameter to true.

  8. Set the action to the form field with the SetFormFieldAction method. It requires the form field and the action IDs.

  9. 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, and Zoom parameters only have an effect when the DestinationType parameter is set to PdfDestinationType.DestinationTypeXYZ.

  • The Zoom parameter is the last parameter executed by the method.

Information

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);
string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);
// Add a form field.
int formID = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue);
// 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)
    ' Create a new action.
    Dim actionID As Integer = gdpicturePDF.NewActionGoToR(PdfDestinationType.DestinationTypeXYZ, @"C:\temp\destination.pdf", false, 3, 0, 0, 5, 0, 1.5f)
    Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
    ' Add a form field.
    Dim formID As Integer = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue)
    ' Set the action to the form field
    gdpicturePDF.SetFormFieldAction(formID, actionID)
    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:

  1. Create a GdPicturePDF object.

  2. Load the PDF file with the LoadFromFile method.

  3. Set the origin of the coordinate system with the SetOrigin method. This method requires the PDFOrigin enumeration.

  4. Set the measurement unit with the SetMeasurementUnit method to specify the form field’s dimensions and position. This method uses the PdfMeasurementUnit enumeration.

  5. Set the PDF page where you want to place the form field with the SelectPage method.

  6. Add any type of form field.

  7. 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 the PdfActionLaunchOperation enumeration. It’s a Windows-specific parameter. If you aren’t sure what value to assign, set it to PdfActionLaunchOperation.ActionLaunchOperationUndefined.

    • NewWindow — A Boolean value that specifies whether to open the destination PDF document in a new window.

  8. Set the action to the form field with the SetFormFieldAction method. It requires the form field and the action IDs.

  9. Save the PDF document to a file with the SaveToFile method.

Information

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);
string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);
// Add a form field.
int formID = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue);
// 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)
    ' Create a new action.
    Dim actionID As Integer = gdpicturePDF.NewActionLaunch("notepad.exe", "", "", PdfActionLaunchOperation.ActionLaunchOperationUndefined, true)
    Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
    ' Add a form field.
    Dim formID As Integer = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue)
    ' Set the action to the form field
    gdpicturePDF.SetFormFieldAction(formID, actionID)
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics

To create an action that opens a website, use the following steps:

  1. Create a GdPicturePDF object.

  2. Load the PDF file with the LoadFromFile method.

  3. Set the origin of the coordinate system with the SetOrigin method. This method requires the PDFOrigin enumeration.

  4. Set the measurement unit with the SetMeasurementUnit method to specify the form field’s dimensions and position. This method uses the PdfMeasurementUnit enumeration.

  5. Set the PDF page where you want to place the form field with the SelectPage method.

  6. Add any type of form field.

  7. 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.

  8. Set the action to the form field with the SetFormFieldAction method. It requires the form field and the action IDs.

  9. Save the PDF document to a file with the SaveToFile method.

Information

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);
string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);
// Add a form field.
int formID = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue);
// 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)
    ' Create a new action.
    Dim actionID As Integer = gdpicturePDF.NewActionURI(@"https:\\pspdfkit.com\", false)
    Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
    ' Add a form field.
    Dim formID As Integer = gdpicturePDF.AddPushButtonFormField(2, 2, 5, 2, "pushButton", "Page 3", fontResName, 12, GdPicture14.Imaging.GdPictureColor.Blue)
    ' Set the action to the form field
    gdpicturePDF.SetFormFieldAction(formID, actionID)
    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:

  1. Create a GdPicturePDF object.

  2. Load the PDF file with the LoadFromFile method.

  3. Set the origin of the coordinate system with the SetOrigin method. This method requires the PDFOrigin enumeration.

  4. Set the measurement unit with the SetMeasurementUnit method to specify the form field’s dimensions and position. This method uses the PdfMeasurementUnit enumeration.

  5. Set the PDF page where you want to place the form field with the SelectPage method.

  6. Add any type of form field.

  7. Create a new action with the NewActionJavaScript method. It uses a string parameter as its parameter, which contains the JavaScript code.

  8. Set the action to the form field with the SetFormFieldAction method. It requires the form field and the action IDs.

  9. Save the PDF document to a file with the SaveToFile method.

Information

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!\");");
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);
// 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)
    ' Create a new action.
    Dim actionID As Integer = gdpicturePDF.NewActionJavaScript("app.alert(\"Hello!\");")
    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)
    ' Set the action to the form field
    gdpicturePDF.SetFormFieldAction(formID, actionID)
    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 a form field, use the GetFormFieldActionID method. It requires the form field 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.GetFormFieldActionID(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.GetFormFieldActionID(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: