Adding Actions to PDF
Add actions to a PDF document to perform tasks in places such as outlines, links, buttons, or in any document, page, or form field. The C# examples below illustrate that adding interactivity to your end user's PDFs is a breeze when using DynamicPDF Core Suite for .NET.
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
Document
object. - Create two
Page
objects and add them to theDocument
instance. - Create a
Button
object. - Create a
GoToAction
object and set its parameters. - Set the button's
ReaderEvents.Mouseup
event to the action. - Add the
Button
instance to thePage
instance. - Add the
Draw
method 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
Document
object. - Create a
Page
object and add it to theDocument
instance. - Create a
Button
object. - Create a
ImportFormDataAction
object by specifying the path to the FDF file in the constructor. - Set the button's
ReaderEvents.Mouseup
event to the action. - Add the
Button
instance to thePage
object. - Add the
Draw
method 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
Document
object. - Create a
Page
object and add it to theDocument
instance. - Create a
ResetAction
object. - Create a
Button
object and set its parameters. - Set the button's
ReaderEvents.MouseEnter
event to the action. - Add the
Button
object to thePage
object instance. - Add the
Draw
method 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
Document
object. - Create a
Page
object and add it to theDocument
object. - Create a
SubmitAction
object and set the URL and response type asFormExportFormat.HtmlPost
. - Create a
Button
object and set its parameters. - Assign the action to
SubmitAction
object. - Add the
Button
object to thePage
instance. - Add the
Draw
method 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
Document
object. - Create a
Page
object and add it to theDocument
object instance. - Create a
UrlAction
object and set its URL. - Create a
Link
object and set its value to the text link that represents the URL. - Add the
Link
object to thePage
instance. - Add the
Draw
method 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
Document
object. - Create a
Page
object and add it to theDocument
instance. - Create a
Button
object. - Create a
FileOpenAction
object and in its constructor specify the file to open and the launch mode (``FileLaunchMode.NewWindow`) - Add the
Button
object to thePage
instance. - Add the
Draw
method 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 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