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:
[Visual Basic]
Dim MyDocument As Document = New Document()
MyDocument.JavaScripts.Add(New DocumentJavaScript("Say Hi", "app.alert(\"HelloNot Not \")"))
[C#]
Document document = new Document();
document.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:
[Visual Basic]
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)
[C#]
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 );
Calling Document Level JavaScript from a Form
[Visual Basic]
MyDocument.JavaScripts.Add(New DocumentJavaScript("Say Bye", "function bye(){app.alert(\"Good ByeNot Not \")}"))
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)
[C#]
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 );
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 .NET

