PDF Java Script
DynamicPDF™ allows you to specify JavaScript code at the document level as well as at the form field level for any PDF.
Document Level JavaScript
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 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:
[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");
// Assign JavaScript action to button action.
button.setAction(action);
page.getElements().add( button );
Calling Document Level JavaScript from a Form
[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 the Adobe’s Acrobat JavaScript Scripting Guide, http://partners.adobe.com/public/developer/en/acrobat/sdk/AcroJSGuide.pdf
| See Also |
DocumentJavaScript Class | JavaScriptAction Class | Button Class | Programming with Generator for Java

