Adding JavaScript

Skip to end of metadata
Go to start of metadata
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 :
  • Acrobat JavaScript does not have access to objects within an HTML page. Similarly, HTML JavaScript cannot access objects within a PDF file.
  • HTML JavaScript is able to manipulate such objects as Window. Acrobat JavaScript cannot access this particular object but it can manipulate PDF-specific objects.
With the release of Aspose.Pdf for .NET version 3.5.2, this component now provides the facility to add Acrobat JavaScript in PDF.

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

  • Declare and Instantiate a JavaScript object
  • Call Add method of JavaScript Class and pass JavaScript statement as an argument to the function.

The following example demonstrates how to add JavaScript in Pdf.

C#
//Instantiate a PDF Object 
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();

//Instantiate a Aspose PDF JavaScript Object
pdf.JavaScripts = new Aspose.Pdf.Generator.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 Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()

'Instantiate a Aspose PDF JavaScript Object
pdf.JavaScripts = New Aspose.Pdf.Generator.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>
 
For further details, please visit JavaScripts
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.