Add JavaScript to PDF Files in Java

PDF is one of the most widely used document formats because it preserves layout across heterogeneous platforms. It can be opened in desktop applications as well as web browsers, and browser support allows PDFs to execute embedded JavaScript. In this article, you will learn how to add or remove JavaScript in PDF files programmatically in Java.

Java API to Add or Remove JavaScript in PDF - Free Download

Aspose.PDF for Java is a comprehensive library that enables developers to generate, edit, and manipulate PDF documents programmatically. It supports adding and removing Acrobat JavaScript at both document and page levels. You can download the library or add it to your Maven project with the following repository and dependency definitions.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>22.4</version>
</dependency>

Add JavaScript to PDF Files in Java

PDF files support Acrobat JavaScript, which is based on the core of JavaScript 1.5 (ISO‑16262). Unlike HTML JavaScript, Acrobat JavaScript interacts directly with PDF objects rather than DOM elements. Understanding this distinction helps you write effective scripts for PDFs.

  • Acrobat JavaScript cannot access HTML page objects, and HTML JavaScript cannot manipulate PDF objects.
  • Acrobat JavaScript works with PDF‑specific objects such as fields, annotations, and actions, while HTML JavaScript manipulates the browser window and DOM.

To embed JavaScript into a PDF with Aspose.PDF for Java, follow these steps:

  1. Load the PDF using the Document class.
  2. Create a JavascriptAction instance and supply the script you want to run.
  3. For document‑level scripts, assign the JavascriptAction to Document.setOpenAction().
  4. For page‑level scripts, use setOnClose() and setOnOpen() on the page’s action collection.
  5. Save the modified PDF with Document.save(String).

The code example below demonstrates how to add both document‑level and page‑level JavaScript.

Java Add Document Level JavaScript in a PDF

Document‑level JavaScript runs automatically when the PDF is opened. Aspose.PDF provides a straightforward API to insert such scripts.

Steps to add a document‑level script:

  1. Open the PDF with Document.
  2. Use Document.getJavaScript().set_Item() to add a named JavaScript function.
  3. Save the file using Document.save(String).

The following snippet adds a simple alert script to the PDF document.

Java Remove JavaScript from a PDF File

If you need to clean up a PDF by removing unwanted scripts, Aspose.PDF lets you delete JavaScript entries by their key.

Procedure to remove JavaScript:

  1. Load the target PDF with Document.
  2. Call Document.getJavaScript().remove(String), passing the script name you wish to delete.
  3. Persist the changes with Document.save(String).

The example below shows how to remove a previously added script.

Java API to Add/Remove JavaScript in PDF - Get a Free License

You can get a free temporary license to evaluate Aspose.PDF for Java without the usual evaluation restrictions. This license enables full access to all PDF manipulation features, including JavaScript handling.

Conclusion

This guide demonstrated how to programmatically add both page‑level and document‑level JavaScript to PDF files using Java, as well as how to remove existing scripts. For deeper insights and additional code samples, explore the official Aspose.PDF for Java documentation. If you have questions or need assistance, feel free to join the discussion on our forum.

See Also