Working with JavaScript

Adding JavaScript (DOM)

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 like 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.

You can add JavaScript at both the document and page levels using Aspose.PDF for .NET. To add JavaScript:

Adding JavaScript to Document or Page Action

  1. Declare and instantiate a JavascriptAction object with desired JavaScript statement as the constructor argument.
  2. Assign the JavascriptAction object to the desired action of the PDF document or page.

The example below applies the OpenAction to a specific document.

Adding/Removing JavaScript to Document Level

A new property named JavaScript is added in Document class which has JavaScript collection type and provides access to JavaScript scenarios by its key. This property is used to add Document level JavaScript. The JavaScript collection has the following properties and methods:

  • string this(string key)– Gets or sets JavaScript by its name.
  • IList Keys – provides a list of existing keys in JavaScript collection.
  • bool Remove(string key) – removes JavaScript by its key.

Setting Expiry Date of a PDF Document Using JavaScript Actions

Aspose.PDF allows you to set an expiry date for a PDF document by embedding JavaScript Actions. This functionality ensures the PDF becomes inaccessible after a specified date and time, enhancing document security and control. By leveraging JavaScript Actions, you can define precise expiration conditions down to the second, ensuring the document’s accessibility is tightly regulated.

You can achieve this by following these steps

  1. Initialize Document: Create a new PDF document and add a blank page or open an existing PDF document.
  2. Define Expiry Date and Time: Set the date and time after which the document will expire.
  3. Prepare JavaScript Code:
    • Retrieve the current date and time.
    • Define the exact expiry date and time, considering that months are zero-based in JavaScript.
    • Compare the current date and time with the expiry date and time.
    • If the current date and time exceed the expiry date and time, display an alert and close the document.
  4. Set Open Action: Associate the JavaScript action with the document’s open action.
  5. Save Document: Save the PDF with the embedded JavaScript that enforces the expiry condition.

Below are code snippets demonstrating this functionality in both C# (.NET) and Java.

The following C# code snippet demonstrates how to set an expiry date and time for a PDF document using JavaScript Actions with Aspose.PDF:

  • JavaScript Date Object: In JavaScript, the month index starts at 0 for January and ends at 11 for December. Ensure that the month value is adjusted accordingly when setting the expiry date and time.

  • Security Considerations: While JavaScript actions can control the behavior of a PDF document, they rely on the PDF viewer’s support for JavaScript. Not all PDF viewers may honor these scripts, and users might have JavaScript execution disabled for security reasons.

  • Customization: Modify the JavaScript code to perform additional actions upon expiry, such as disabling certain features, redirecting to a specific page, or logging the event. Additionally, if necessary, you can check only the date of expiry without specifying the time.