Add Reader Events to PDFs
Add interactivity to a PDF by adding reader events to a document, page, or form fields using DynamicPDF Core Suite for .NET. The following C# examples show you how.
How to Add Document Reader Event to PDF in C#
The following steps and C# sample code illustrate adding a reader event to a document using DynamicPDF Core Suite for .NET so that events are triggered at the document level.
Steps for Adding Document Reader Event to PDF Document
- Create a
Document
object. - Create a
JavaScriptAction
object and provide the script to be executed. - Assign the action to the
Document
instance'sReaderEvents
property'sDocumentReaderEvents.WillSave
event. - Save the PDF document.
Sample Code - C#
Document document = new Document();
JavaScriptAction action = new JavaScriptAction("app.alert(\"Hello your text was Saved!\")");
document.ReaderEvents.WillSave = action;
document.Draw( "output.pdf" );
How to Add Page Reader Event to PDF in C#
The following steps and C# sample code illustrate adding a reader event to a page using DynamicPDF Core Suite for .NET.
Steps for Adding Page Reader Event to PDF Document
- Create a
Document
object. - Create a
Page
object and add it to theDocument
instance. - Create a
JavaScriptAction
object and provide the script to be executed. - Assign the action to the
Page
instance'sReaderEvents
property'sPageReaderEvents.Open
event. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add( page );
JavaScriptAction action = new JavaScriptAction("app.alert(\"Welcome !\")");
page.ReaderEvents.Open = action;
document.Draw( "output.pdf" );
How to Add Form Field Reader Event to PDF in C#
The following steps and C# sample code illustrate adding a form field event to a PDF document using DynamicPDF Core Suite for .NET.
Steps for Adding Form Field Reader Event to a PDF Document
- Create a
Document
object. - Create a
Page
object and add it to theDocument
. - Create a
TextField
object. - Create a
JavaScriptAction
object and provide the script to be executed. - Assign the action to the
TextField
instance'sReaderEvents
property'sAnnotationReaderEvents.OnFocus
event. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add( page );
TextField textField = new TextField("Text1", 0, 0, 100, 15);
JavaScriptAction action = new JavaScriptAction("app.alert(\"Welcome !\")");
textField.ReaderEvents.OnFocus = action;
page.Elements.Add(textField);
document.Draw("output.pdf" );
GitHub Project
Examples are provided in C# and VB.NET. Clone or view the example project at GitHub. This example code is contained in the following files.
- C# - ReaderEventExample.cs
- VB.NET - ReaderEventExample.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 Reader Events
- Refer to the Events documentation, or
- Chapter 16, Making PDFs Interactive, in the book DynamicPDF Core Suite for .NET by Example available on GitHub.
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