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 annotation’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the annotation 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.RetainTop— Optional: To keep the top 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:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager license = new LicenseManager();license.RegisterKEY("");{using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.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);// Add a free text annotation.int annotID = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, true, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1f);// Set the action to the annotation.gdpicturePDF.SetAnnotationAction(annotID, actionID);gdpicturePDF.SaveToFile(@"output.pdf");}Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports GdPicture14
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.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) ' Add a free text annotation. Dim annotID As Integer = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, True, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1F) ' Set the action to the annotation. gdpicturePDF.SetAnnotationAction(annotID, actionID) gdpicturePDF.SaveToFile("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 annotation’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the annotation 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.RetainTop— Optional: To keep the top 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(@"input.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);// Add a free text annotation.int annotID = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, true, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1f);// Set the action to the annotation.gdpicturePDF.SetAnnotationAction(annotID, actionID);gdpicturePDF.SaveToFile(@"output.pdf");}Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.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) ' Add a free text annotation. Dim annotID As Integer = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, True, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1F) ' Set the action to the annotation. gdpicturePDF.SetAnnotationAction(annotID, actionID) gdpicturePDF.SaveToFile("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 annotation’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the annotation 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 launches Notepad, use the following code:
{using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.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);// Add a free text annotation.int annotID = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, true, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1f);// Set the action to the annotation.gdpicturePDF.SetAnnotationAction(annotID, actionID);gdpicturePDF.SaveToFile(@"output.pdf");}Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.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) ' Add a free text annotation. Dim annotID As Integer = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, True, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1F) ' Set the action to the annotation. gdpicturePDF.SetAnnotationAction(annotID, actionID) gdpicturePDF.SaveToFile("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 annotation’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the annotation with the
SelectPagemethod. - Add any type of annotation.
- Create a new action with the
NewActionURImethod. 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 opens a website, use the following code:
{using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.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://www.nutrient.io/", false);// Add a free text annotation.int annotID = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, true, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1f);// Set the action to the annotation.gdpicturePDF.SetAnnotationAction(annotID, actionID);gdpicturePDF.SaveToFile(@"output.pdf");}Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.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://www.nutrient.io/", False) ' Add a free text annotation. Dim annotID As Integer = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, True, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1F) ' Set the action to the annotation. gdpicturePDF.SetAnnotationAction(annotID, actionID) gdpicturePDF.SaveToFile("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 annotation’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Set the PDF page where you want to place the annotation 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 NewActionJavaScript method is the action’s ID. It’s required to assign the trigger element of the newly created action.
To create an action that runs JavaScript, use the following code:
{using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.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!\");");// Add a free text annotation.int annotID = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, true, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1f);// Set the action to the annotation.gdpicturePDF.SetAnnotationAction(annotID, actionID);gdpicturePDF.SaveToFile(@"output.pdf");}Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.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!"");") ' Add a free text annotation. Dim annotID As Integer = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, True, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1F) ' Set the action to the annotation. gdpicturePDF.SetAnnotationAction(annotID, actionID) gdpicturePDF.SaveToFile("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(@"input.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);// Add a free text annotation.int annotID = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, true, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1f);// Create a new action and set it to the annotation.int newActionID = gdpicturePDF.NewActionURI("https://www.nutrient.io/", false);gdpicturePDF.SetAnnotationAction(annotID, newActionID);// Get the ID of the action associated with the annotation.int actionID = gdpicturePDF.GetAnnotationActionID(annotID);Console.WriteLine($"Action ID: {actionID}");}Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.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) ' Add a free text annotation. Dim annotID As Integer = gdpicturePDF.AddFreeTextAnnotation(2, 2, 5, 2, True, "Action", "Action", "Open", "", 12, 0, 0, 200, 100, 100, 100, 1F) ' Create a new action and set it to the annotation. Dim newActionID As Integer = gdpicturePDF.NewActionURI("https://www.nutrient.io/", False) gdpicturePDF.SetAnnotationAction(annotID, newActionID) ' Get the ID of the action associated with the annotation. Dim actionID As Integer = gdpicturePDF.GetAnnotationActionID(annotID) Console.WriteLine($"Action ID: {actionID}")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.