Adding Actions to a PDF
Adding actions to a PDF document is a breezed when using DynamicPDF Core Suite for .NET. Use actions to perform tasks in places such as outlines, links, buttons, or in any document, page, or form field. The C# examples below show you how.
How to Add GotoAction to PDF
Use the GoToAction to navigate to a particular page on a PDF document. The following steps and C# sample code illustrates the action by creating two pages and adding an action that navigates between the two pages.
Steps for Adding GotoAction to a PDF Document
- Create a
Documentobject. - Create two
Pageobjects and add them to theDocumentinstance. - Create a
Buttonobject. - Create a
GoToActionobject and set its parameters. - Set the button's
ReaderEvents.Mouseupevent to the action. - Add the
Buttoninstance to thePageinstance. - Add the
Drawmethod to output the PDF document.
Sample Code - C#
public static void GoToAction()
{
Document document = new Document();
Page page1 = new Page();
document.Pages.Add(page1);
Page page2 = new Page();
page2.Elements.Add(new Label("Page 2", 0, 0, 100, 15));
document.Pages.Add(page2);
Button button = new Button("btn", 50, 150, 100, 30);
button.Label = "Click Here";
GoToAction action = new GoToAction(2, PageZoom.FitWidth);
button.ReaderEvents.MouseUp = action;
page1.Elements.Add(button);
document.Draw("output.pdf");
}
How to Add ImportFormDataAction to PDF
The ImportFormDataAction fills a form's fields by importing data from a Forms Data Format (FDF) file. The following steps and C# sample code illustrate adding an ImportFormDataAction to a PDF document.
Steps for Adding ImportFormDataAction
- Create a
Documentobject. - Create a
Pageobject and add it to theDocumentinstance. - Create a
Buttonobject. - Create a
ImportFormDataActionobject by specifying the path to the FDF file in the constructor. - Set the button's
ReaderEvents.Mouseupevent to the action. - Add the
Buttoninstance to thePageobject. - Add the
Drawmethod to output the PDF document.
Note: Importing form data expects the form's data to be in Forms Data Format (FDF).
Sample Code - C#
public static void FormFillingFdfDataDocument()
{
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);
Button button = new Button("btn", 50, 250, 100, 30);
button.Label = "Fill Form";
page.Elements.Add(button);
ImportFormDataAction action = new ImportFormDataAction("simple-form-fill_data.fdf");
button.ReaderEvents.MouseUp = action;
document.Draw("formfilling-output.pdf");
}
How to Add ResetAction to PDF
The ResetAction resets all form field values to their default values. The following steps and C# sample code illustrate resetting a form.
Steps for Adding ResetAction to a PDF Document
- Create a
Documentobject. - Create a
Pageobject and add it to theDocumentinstance. - Create a
ResetActionobject. - Create a
Buttonobject and set its parameters. - Set the button's
ReaderEvents.MouseEnterevent to the action. - Add the
Buttonobject to thePageobject instance. - Add the
Drawmethod to output the PDF document.
Sample Code - C#
public static void ResetAction()
{
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);
ResetAction action = new ResetAction();
Button button = new Button("btn", 50, 300, 100, 50);
button.Label = "Reset";
button.ReaderEvents.MouseEnter = action;
page.Elements.Add(button);
document.Draw("ResetAction.pdf");
}
How to Add SubmitAction to PDF
The SubmitAction sends data in form fields to a specified URL. The following steps and C# sample code illustrates this action.
Steps for Adding SubmitAction to a PDF Document
- Create a
Documentobject. - Create a
Pageobject and add it to theDocumentobject. - Create a
SubmitActionobject and set the URL and response type asFormExportFormat.HtmlPost. - Create a
Buttonobject and set its parameters. - Assign the action to
SubmitActionobject. - Add the
Buttonobject to thePageinstance. - Add the
Drawmethod to output the PDF document.
Sample Code - C#
public static void SubmitAction()
{
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);
SubmitAction submitAction = new SubmitAction("http://www.mydomain.com", FormExportFormat.HtmlPost);
Button button = new Button("btn", 50, 300, 100, 50);
button.Label = "Submit";
button.Action = submitAction;
page.Elements.Add(button);
document.Draw("output.pdf");
}
How to Add URLAction to PDF
The URLAction links a web URL destination within a PDF document. The following steps and C# sample code add a URLAction to a PDF document using DynamicPDF Core Suite for .NET.
Steps for Adding a URLAction to a PDF Document
- Create a
Documentobject. - Create a
Pageobject and add it to theDocumentobject instance. - Create a
UrlActionobject and set its URL. - Create a
Linkobject and set its value to the text link that represents the URL. - Add the
Linkobject to thePageinstance. - Add the
Drawmethod to output the PDF document.
Sample Code - C#
public static void UrlAction()
{
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);
UrlAction action = new UrlAction("http://www.dynamicpdf.com");
Link link = new Link(50, 50, Font.Helvetica.GetTextWidth("My Domain", 18), 20, action);
Button button = new Button("btn", 50, 100, 100, 50);
button.Label = "Click";
button.Action = action;
page.Elements.Add(link);
page.Elements.Add(button);
document.Draw("output.pdf");
}
How to Add FileOpenAction to PDF
The FileOpenAction opens a file, provided a native application to open the specific file is already installed. The steps and C# sample code that follows illustrates adding a FileOpenAction to a PDF document using the DynamicPDF Core Suite.
Steps for Adding the FileOpenAction to a PDF Document
- Create a
Documentobject. - Create a
Pageobject and add it to theDocumentinstance. - Create a
Buttonobject. - Create a
FileOpenActionobject and in its constructor specify the file to open and the launch mode (``FileLaunchMode.NewWindow`) - Add the
Buttonobject to thePageinstance. - Add the
Drawmethod to output the PDF document.
Sample Code - C#
public static void FileOpenAction()
{
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);
Button button = new Button("btn", 50, 150, 100, 30);
button.Label = "Click Here";
FileOpenAction action = new FileOpenAction("DocumentA.pdf", FileLaunchMode.NewWindow);
button.ReaderEvents.MouseUp = action;
age.Elements.Add(button);
document.Draw("output.pdf");
}
GitHub Project
An example project is available on GitHub (examples.dynamicpdf-core-suite-dotnet-core). Examples are provided in C# and VB.NET. Clone or view the example project at GitHub. This specific example discussed on this page are all contained in following classes on GitHub.
- C# - Actions.cs
- C# - FormFdfExample.cs
- VB.NET - Actions.vb
- VB.NET - FormFdfExample.vb
Getting Started
Get started easily by installing DynamicPDF Core Suite for .NET through NuGet or manually. Then, refer to the documentation for more information on using and purchasing the product.
NuGet Package
The easiest way to install DynamicPDF Core Suite is by obtaining the NuGet package using Visual Studio's Package Manager. You can also obtain the NuGet package by downloading it directly. Refer to the installation documentation for more information.
DynamicPDF Core Suite for .NET Information
DynamicPDF Core Suite for .NET combines creating, merging, and visual report creation into one powerful product for creating PDF documents. It is ideal for anyone who needs to generate PDF documents or reports or work with existing PDFs in their applications. With a free Evaluation Edition to try and with flexible and royalty-free licensing options, why not start using DynamicPDF Core Suite for .NET today!
More Information on Actions
- Refer to the Actions documentation topic, or
- Chapter 16, Making PDFs Interactive, in the book DynamicPDF Core Suite for .NET by Example available on GitHub.
Also See
- See Adding Show/Hide Action to PDF for an example showing how to use the
AnnotationShowHideAction. - See Adding JavaScript Actions to PDF for an example showing how to use the
JavaScriptAction.
Available on Other Platforms
DynamicPDF Core Suite is also available for the Java and COM/ActiveX platforms. Refer to the respective product pages for more details.
- DynamicPDF Generator
- Java - DynamicPDF Generator for Java
- COM/ActiveX - DynamicPDF Generator for COM/ActiveX
- DynamicPDF Merger
- Java - DynamicPDF Merger for Java
- COM/ActiveX - DynamicPDF Merger for COM/ActiveX