Aspose.Pdf

Adding JavaScript

WHAT IS ACROBAT JAVASCRIPT?

 

Acrobat JavaScript is a language based on the core of JavaScript version 1.5 of ISO-16262, formerly known as ECMAScript, an object-oriented scripting language developed by Netscape Communications. JavaScript was created to offload Web page processing from a server onto a client in Web-based applications. Acrobat JavaScript implements extensions, in the form of new objects and their accompanying methods and properties, to the JavaScript language. These Acrobat-specific objects enable a developer to manage document security, communicate with a database, handle file attachments, manipulate a PDF file so that it behaves as an interactive, web-enabled form, and so on. Because the Acrobat-specific objects are added on top of core JavaScript, you still have access to its standard classes, including Math, String, Date, Array, and RegExp.

 

ACROBAT JAVASCRIPT Vs HTML(WEB) JAVASCRIPT

 

PDF documents have great versatility since they can be displayed both within the Acrobat software as well as a Web browser. Therefore, it is important to be aware of the differences between Acrobat JavaScript and JavaScript used in a Web browser, also known as HTML JavaScript:

 

 

Note: With the release of Aspose.Pdf version 3.5.2 it provides the facility to add Acrobat JavaScript in PDF.

 

Please follow the following two steps to add JavaScipts in your PDF using Aspose.Pdf :

 

 

The following example demostrates how to add JavaScript in Pdf.

 

[C#]

 

//Instantiate a PDF Object

Pdf pdf = new Pdf();

 

//Instantiate a Aspose PDF JavaScript Object

pdf.JavaScripts = new JavaScripts();

 

//Call the Add method and pass JavaScript statement as an argument, to show Print Dialog

pdf.JavaScripts.Add("this.print(true);");

 

//Call the Add method and JavaScript statement as an argument, to show alert

pdf.JavaScripts.Add("app.alert(\"hello world\");");

 

//Save Pdf Document

pdf.Save(@"d:\test\test.pdf");

 

[VB.Net]

 

'Instantiate a PDF Object

Dim pdf As Pdf =  New Pdf()

 

'Instantiate a Aspose PDF JavaScript Object

pdf.JavaScripts = New JavaScripts()

 

'Call the Add method and pass JavaScript statement as an argument, to show Print Dialog

pdf.JavaScripts.Add("this.print(true);")

 

'Call the Add method and JavaScript statement as an argument, to show alert

pdf.JavaScripts.Add("app.alert(\"hello world\");")

 

'Save Pdf Document

pdf.Save("d:\test\test.pdf")

 

To Add JavaScript using the XML. Please check the following:

 

[XML]

 

<Pdf xmlns="Aspose.Pdf">

<JavaScript ID="js1">this.print(true);</JavaScript>

<JavaScript>app.alert("hello world");</JavaScript>

</Pdf>

 

To get furthur details about JavaScripts please visit our online API documents.