How to Add Document Reader Event to PDF in C#
Adding a Document Reader Event to a PDF using DynamicPDF Core Suite is straightforward. The following steps and sample code illustrate adding a Document Reader Event to a PDF document using DynamicPDF Core Suite. The following events can be triggered at the document level.
WillSave
- This event will be fired when saving a document.DidSave
- This event will be fired after saving a document.WillClose
- This event will be fired while closing a document.WillPrint
- This event will be fired when printing a document.DidPrint
- This event will be fired after printing a document.
The example below demonstrates how to trigger a JavaScript alert window when saving a PDF.
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.
- Save the PDF document.
Sample Code - C#
Document document = new Document();
JavaScriptAction action = new JavaScriptAction("app.alert(\"Hello your text Saved!!\")");
document.ReaderEvents.WillSave = action;
document.Draw( "output.pdf" );
How to Add Page Reader Event to PDF in C#
The following steps and sample code illustrate adding a page ReaderEvent
to a PDF document using DynamicPDF Core Suite. The following events can be triggered for each page in a PDF.
Open
- This event is fired when opening a specific page.Close
- This event is fired when closing a specific page.
The example below demonstrates how to trigger a JavaScript alert window when a specific page is opened.
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. - 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 sample code illustrate adding a Form Field Reader Event to a PDF document using DynamicPDF Core Suite. The following events can be triggered for each form field.
MouseUp
- This event is fired when the mouse button is released after a click.MouseDown
- This event is fired when the mouse button is clicked and not released.MouseEnter
- This event is fired when the mouse pointer enters the field.MouseExit
- This event is fired when the pointer exits the form field.OnBlur
- This event is fired when the form field loses focus.OnFocus
- This event is fired when the form field receives focus, either through a mouse action or by tabbing.
The example below demonstrates how to trigger a JavaScript alert window when a text field receives focus.
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
. - 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
Clone or view the example project at GitHub. This example code is contained in the Examples/ReaderEventExample.cs file.
Getting Started
NuGet Package
DynamicPDF Core Suite is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET
package. The easiest way to install the package is through the Visual Studio Package Manager. You can also download the package directly from NuGet.
DynamicPDF Core Suite Information
More information can be found on the DynamicPDF Core Suite webpage.
Available on Other Platforms
DynamicPDF Core Suite is available for the Java and COM/ActiveX platforms. Refer to the respective product pages for more details.
- Java - DynamicPDF Generator for Java
- COM/ActiveX - DynamicPDF Generator for COM/ActiveX