Add JavaScript to PDF Files in Java

PDF is one of the widely used document formats because of its consistent layout across heterogeneous platforms. Furthermore, it can be displayed in desktop-based software applications as well as the web browsers at the same time. The support of web browsers make PDF files capable of running 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 an amazing API that provides a range of features for PDF generation and manipulation. With this API, you can seamlessly create new and manipulate the existing PDF files. We will use this API to add and remove JavaScript in PDF files. You can download the API or install it using the following Maven configurations.

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 version 1.5 of ISO-16262, formerly known as ECMAScript. It is an object-oriented scripting language developed by Netscape Communications. Before proceeding to add JavaScript to PDF files, let’s have a look at the differences between Acrobat JavaScript and HTML JavaScript which is used in web browsers.

  • 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 objects such as Window. However, Acrobat JavaScript cannot access these particular objects but it can manipulate PDF-specific objects.

The following are the steps to add JavaScript to a PDF file.

The following code sample shows how to add JavaScript to a PDF file.

Java Add Document Level JavaScript in a PDF

You can also add JavaScript to the document level using Document.getJavaScript().set_Item() method. The following are the steps to add JavaScript to the document level in Java.

The following code sample shows how to add JS script to a PDF file in Java.

Java Remove JavaScript from a PDF File

The following are the steps to remove JavaScript from a PDF file in Java.

The following code sample shows how to remove JavaScript from a PDF file in Java.

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

You can get a free temporary license in order to use Aspose.PDF for Java without evaluation limitations.

Conclusion

In this article, you have learned how to add JavaScript to PDF files in Java. Furthermore, you have seen how to remove JavaScript from a PDF file programmatically. Besides, you can explore more about Java PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.

See Also