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 AnnotationReaderEvents , PageReaderEvents and DocumentReaderEvents.
The Document object contains a list of all JavaScript for that particular PDF. This list is referenced using the Document’s JavaScripts property. The following code demonstrates adding a simple entry to the document level JavaScript for the PDF:
[Java]
Document document = new Document();
document.getJavaScripts().add( new DocumentJavaScript( "Say Hi", "app.alert(\"Hello!!\")" ) );
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:
[Java]
JavaScriptAction action = new JavaScriptAction("this.print({bUI: false, bSilent: true, bShrinkToFit: true});");
Button button = new Button( "Button Name", 200, 200, 100, 25 );
button.setBehavior( Behavior.PUSH );
button.setLabel("Submit");
button.setAction(action);
page.getElements().add( button );
Form fields such as Buttons, TextFields, ListBoxes, RadioButtons etc... can also be associated with a JavaScript actions using AnnotationReaderEvents. The JavaScript will be executed based on the Annotation reader event selected.
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.
[Java]
Document document = new Document();
Page page = new Page(PageSize.LETTER);
Label label1 = new Label("Please Enter your Date of Birth :", 50, 50, 200, 50);
TextField textField1 = new TextField("dob", 270, 50, 100, 50);
textField1.getReaderEvents().setOnblur(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.getElements().add(label1);
page.getElements().add(textField1);
Label label2 = new Label("Your Age is :", 50, 120, 100, 50);
TextField textField2 = new TextField("age", 270, 120, 100, 50);
page.getElements().add(label2);
page.getElements().add(textField2);
document.getPages().add(page);
document.draw("output.pdf");
In the following example using an OnBlur event is validating the field value and throwing exception if it is not a number.
[Java]
Document document = new Document();
Page page = new Page(PageSize.LETTER);
Label label = new Label("Please Enter a Number :", 0, 50, 150, 30);
TextField textField = new TextField("txt", 170, 30, 150, 30);
textField.setDefaultValue("0");
textField.setToolTip("Enter only Numbers");
textField.getReaderEvents().setOnblur(new JavaScriptAction(" var no = this.getField(\"txt\").value; if( isNaN(no)) { app.alert(\"Please Enter number in the text field\"); } "));
page.getElements().add(textField);
page.getElements().add(label);
document.getPages().add(page);
document.draw("output.pdf");
In the following example using an OnBlur event, the field value is formatted to two decimal places.
[Java]
Document document = new Document();
Page page = new Page(PageSize.LETTER);
Label label1 = new Label("Please Enter a Number :", 50, 50, 200, 50);
TextField textField1 = new TextField("number", 270, 50, 100, 50);
textField1.getReaderEvents().setOnblur(new JavaScriptAction(" var no = this.getField(\"number\").value; this.getField(\"number\").value = no.toFixed(2) ; "));
page.getElements().add(label1);
page.getElements().add(textField1);
document.getPages().add(page);
document.draw("output.pdf");
[Java]
document.getJavaScripts().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.setBehavior(Behavior.PUSH);
button.setLabel("Say Bye");
button.setAction(action);
page.getElements().add( button );
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