Javascript

DynamicPDF Core Suite supports adding JavaScript to PDF documents.

PDFJavaScript

DynamicPDF allows you to specify JavaScript code at the document level as well as at the form field level for any PDF. Javascript can be used along with AnnotationReaderEventsPageReaderEvents and DocumentReaderEvents.

Document Level JavaScript

The Document object contains a list of all JavaScript for that particular PDF. This list is referenced using the Documents JavaScripts property. The following code demonstrates adding a simple entry to the document level JavaScript for the PDF:

Document document = new Document();
document.JavaScripts.Add( new DocumentJavaScript( "Say Hi", "app.alert(\"Hello!!\")" ) ); 
Dim MyDocument As Document =  New Document() 
MyDocument.JavaScripts.Add(New DocumentJavaScript("Say Hi", "app.alert(\"Hello!!\")"))  

Form Level JavaScript

Form fields such as buttons can also be associated with a JavaScript action. Below is an example that demonstrates a button press executing a JavaScript print action:

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";
// Assign JavaScript action to button action.
button.Action = action;
page.Elements.Add( button );        
Dim MyAction As JavaScriptAction =  New JavaScriptAction("this.print({bUI: false, bSilent: true, bShrinkToFit: true});") 
Dim MyButton As Button =  New Button("Button Name",200,200,100,25) 
MyButton.Behavior = Behavior.Push 
MyButton.Label = "Submit"
' Assign JavaScript action to button action.
MyButton.Action = MyAction
MyPage.Elements.Add(MyButton)   

Form Field Level JavaScript

Form fields such as Button, TextField, ListBox, and RadioButton can also be associated with a JavaScript actions using AnnotationReaderEvents. The JavaScript will be executed based on the Annotation reader event selected.

Creating AutoFillable Form fields using Javascript

In the following example using an OnBlur event, the age text field value will be calculated automatically based on the value entered in the date of birth text field.

Document document = new Document();
Page page = new Page(PageSize.Letter);
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.Pages.Add(page);
document.Draw(@"output.pdf");
Dim MyDocument As Document = New Document()
Dim MyPage As Page = New Page(PageSize.Letter)
Dim MyLabel1 As ceTe.DynamicPDF.PageElements.Label = New ceTe.DynamicPDF.PageElements.Label("Please Enter your Date of Birth :", 50, 50, 200, 50)
Dim MyTextField1 As TextField  = New TextField("dob", 270, 50, 100, 50)
MyTextField1.ReaderEvents.OnBlur = New JavaScriptAction(" var no = this.getField(\"dob\").value
temp = Math.ABS(New Date(Date.Now()).GetTime() - New Date(no).GetTime())
days = Math.CEIL(temp / (1000 \* 3600 \* 24))
Me.GetField(\"age\").value = Math.Floor(days/365) ")
MyPage.Elements.Add(MyLabel1)
MyPage.Elements.Add(MyTextField1)
MyLabel2 As ceTe.DynamicPDF.PageElements.Label = New ceTe.DynamicPDF.PageElements.Label("Your Age is :", 50, 120, 100, 50)
Dim MyTextField2 As TextField = New TextField("age", 270, 120, 100, 50)
MyPage.Elements.Add(MyLabel2)
MyPage.Elements.Add(MyTextField2)
MyDocument.Pages.Add(MyPage)
MyDocument.Draw("output.pdf")

Creating form fields with Javascript for field value validation

In the following example using an OnBlur event is validating the field value and throwing exception if it is not a number.

Document document = new Document();
Page page = new Page(PageSize.Letter);
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.Pages.Add(page);
document.Draw(@"output.pdf");     
Dim MyDocument As Document = New Document()
Dim MyPage As Page = New Page(PageSize.Letter)
Dim MyLabel As Label = New Label("Please Enter a Number :", 0, 50, 150, 30)
Dim MyTextField As ceTe.DynamicPDF.PageElements.Forms.TextField = New ceTe.DynamicPDF.PageElements.Forms.TextField("txt", 170, 30, 150, 30)
MyTextField.DefaultValue = "0"
MyTextField.ToolTip = "Enter only Numbers"
MyTextField.ReaderEvents.OnBlur = New JavaScriptAction(" var no = Me.GetField(\"txt\").value
if( IsNaN(no)) 
app.alert(\"Please Enter number in the text field\") ")
MyPage.Elements.Add(MyTextField)
MyPage.Elements.Add(MyLabel)
MyDocument.Pages.Add(MyPage)
MyDocument.Draw("output.pdf")  

Creating form fields with Javascript for field value formatting

In the following example using an OnBlur event, the field value is formatted to two decimal places.

Document document = new Document();
Page page = new Page(PageSize.Letter);
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.Pages.Add(page);
document.Draw(@"output.pdf");
Dim MyDocument As Document = New Document()
Dim MyPage As Page = New Page(PageSize.Letter)
Dim MyLabel1 As ceTe.DynamicPDF.PageElements.Label = New ceTe.DynamicPDF.PageElements.Label("Please Enter a Number :", 50, 50, 200, 50)
Dim MyTextField1 As TextField = New TextField("number", 270, 50, 100, 50)
MyTextField1.ReaderEvents.OnBlur = New JavaScriptAction(" var no = this.getField(\"number\").value
Me.GetField(\"number\").value = no.ToFixed(2)  ")
MyPage.Elements.Add(MyLabel1)
MyPage.Elements.Add(MyTextField1)
MyDocument.Pages.Add(MyPage)
MyDocument.Draw("output.pdf")         

Calling Document Level JavaScript from a Form

This final example illustrates calling JavaScript from within a form at the document level.

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 );        
MyDocument.JavaScripts.Add(New DocumentJavaScript("Say Bye", "function bye(){app.alert(\"Good Bye!!\")}"))
Dim MyAction As JavaScriptAction =  New JavaScriptAction("bye()") 
Dim MyButton As Button =  New Button("Button Name",200,300,100,25) 
MyButton.Behavior = Behavior.Push 
MyButton.Label = "Say Bye"
MyButton.Action = MyAction
MyPage.Elements.Add(MyButton)    

For further reading on PDF JavaScript please refer to Adobe's Acrobat JavaScript Scripting Guide, https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/js_api_reference.pdf

In this topic