Skip to main content

JavaScript

Version: 7.0

Javascript

PDFJavaScript

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 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)

Calling Document Level JavaScript from a Form​

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, http://partners.adobe.com/public/developer/en/acrobat/sdk/AcroJSGuide.pdf.