Javascript Actions in PDF
Create auto-fillable, self formatting and validating form fields using JavaScript in a PDF using DynamicPDF Core Suite for .NET. These actions can be used to perform tasks in many places such as in outlines, links, buttons or in any Document, Page or Form Field Reader Events.
- Add AutoFillable Form fields using Javascript to PDF
- Add Formfields with Javascript for field value formatting to PDF
- Add Formfields with Javascript for field value validation to PDF
- Add Form level Javascript Action to PDF
- Add Document level Javascript Action to PDF
- How to Call Document Level JavaScript from a Form
- GitHub Project
- Getting Started
- Available in Other Platforms
How to Add AutoFillable Form fields Using Javascript to a PDF in C#
The following steps and sample code illustrate adding an AutoFillable Form field using Javascript to a PDF document using DynamicPDF Core Suite.
Steps for Adding an AutoFillable Form Field using Javascript to a PDF Document
- Create a
Document
object. - Create a
Page
object and add it to theDocument
. - Create a
TextField
object and assign javascript to fill another Formfield, for OnBlue event using theJavaScriptAction
object. - Create the
TextField
object that needs to be updated automatically based on the event. - Add all the Formfields to the
Page
. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add( page );
ceTe.DynamicPDF.PageElements.Label label1 = new ceTe.DynamicPDF.PageElements.Label("Please Enter your Date of Birth :", 50, 50, 200, 50);
TextField textField1 = new TextField("dob", 270, 50, 100, 50);
textField1.ReaderEvents.OnBlur = new JavaScriptAction(" var no = this.getField(\"dob\").value; var temp = Math.abs(new Date(Date.now()).getTime() - new Date(no).getTime()); var days = Math.ceil(temp / (1000 * 3600 * 24));
this.getField(\"age\").value = Math.floor(days/365); ");
page.Elements.Add(label1);
page.Elements.Add(textField1);
ceTe.DynamicPDF.PageElements.Label label2 = new ceTe.DynamicPDF.PageElements.Label("Your Age is :", 50, 120, 100, 50);
TextField textField2 = new TextField("age", 270, 120, 100, 50);
page.Elements.Add(label2);
page.Elements.Add(textField2);
document.Draw( "output.pdf" );
How to Add Formfields with Javascript for a Field Value Formatted to a PDF in C#
The following steps and sample code illustrates adding a Formfield with JavaDcript for a field value formatting to a PDF document.
Steps for Adding Formfields to a PDF in JavaScript for field value formatting to a PDF Document
- Create a
Document
object. - Create a
Page
object and add it to theDocument
. - Create a
TextField
object and assign javascript to format the input, for OnBlue event using theJavaScriptAction
object. - Add all the Formfields to the
Page
. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add( page );
ceTe.DynamicPDF.PageElements.Label label1 = new ceTe.DynamicPDF.PageElements.Label("Please Enter a Number :", 50, 50, 200, 50);
TextField textField1 = new TextField("number", 270, 50, 100, 50);
textField1.ReaderEvents.OnBlur = new JavaScriptAction(" var no = this.getField("number").value; this.getField(\"number").value = no.toFixed(2) ; ");
page.Elements.Add(label1);
page.Elements.Add(textField1);
document.Draw( "output.pdf" );
How to Add Formfields with Javascript for Field Value Validation to a PDF in C#
The following steps and sample code illustrates adding a Formfields with Javascript for field value validation to PDF document using DynamicPDF Core Suite.
Steps for Adding Formfields with Javascript for field value validation to a PDF Document
- Create a
Document
object. - Create a
Page
object and add it to theDocument
. - Create a
TextField
object and assign javascript to validate input, for OnBlue event using theJavaScriptAction
object. - Add all the Formfields to the
Page
. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add( page );
Label label = new Label("Please Enter a Number :", 0, 50, 150, 30);
ceTe.DynamicPDF.PageElements.Forms.TextField textField = new ceTe.DynamicPDF.PageElements.Forms.TextField("txt", 170, 30, 150, 30);
textField.DefaultValue = "0";
textField.ToolTip = "Enter only Numbers";
textField.ReaderEvents.OnBlur = new JavaScriptAction(" var no = this.getField(\"txt\").value; if( isNaN(no)) { app.alert(\"Please Enter number in the text field\"); } ");
page.Elements.Add(textField);
page.Elements.Add(label);
document.Draw( "output.pdf" );
How to add Form level Javascript Action to PDF in C#
Below are the steps and sample code to add a Form level Javascript Action to PDF document using DynamicPDF Core Suite.
Steps for Adding Form level Javascript Action to a PDF Document
- Create a
Document
object. - Create a
Page
object and add it to theDocument
. - Create a
JavaScriptAction
object and provide the script to be executed. - Create a
Button
object and assign Javascript to the button action. - Add the
Button
to thePage
. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add( page );
JavaScriptAction action = new JavaScriptAction("this.print({bUI: false, bSilent: true, bShrinkToFit: true});");
Button button = new Button( "Button Name", 200, 200, 100, 25 );
button.Behavior = Behavior.Push;
button.Label = "Submit";
button.Action = action;
page.Elements.Add( button );
document.Draw( "output.pdf" );
How to add Document level Javascript Action to PDF in C#
Below are the steps and sample code to add a Document level JavaScript Action to PDF document using DynamicPDF Core Suite.
Steps for Adding Document level Javascript Action to a PDF Document
- Create a
Document
object. - Add the javaScript to the document by creating and passing
DocumentJavaScript
object. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);
document.JavaScripts.Add( new DocumentJavaScript( "Say Hi", "app.alert(\"Hello!!\")" ) );
document.Draw( "output.pdf" );
How to Call Document Level JavaScript from a Form in C#
Below are the steps and sample code to call Document Level Javascript from a form in PDF document using DynamicPDF Core Suite.
Steps for Calling Document Level JavaScript from a Form in a PDF Document
- Create a
Document
object. - Create a
Page
object and add it to theDocument
. - Create a
DocumentJavaScript
object and assign the script to be executed. - Create a
JavaScriptAction
object and set the parameters. - Create a
Button
object to trigger the action. - Add the
Button
to thePage
. - Save the PDF document.
Sample Code - C#
Document document = new Document();
Page page = new Page();
document.Pages.Add( page );
document.JavaScripts.Add( new DocumentJavaScript( "Say Bye", "function bye(){app.alert(\"Good Bye!!\")}" ) );
JavaScriptAction action = new JavaScriptAction("bye()");
Button button = new Button( "Button", 200, 300, 100, 25 );
button.Behavior = Behavior.Push;
button.Label = "Say Bye";
button.Action = action;
page.Elements.Add( button );
document.Draw(@"Output.pdf");
GitHub Project
Clone or view the example project at GitHub. This example code is contained in the Examples/JavascriptActionExample.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