Add or Remove JavaScript in PDF Files in C# .NET

Add JavaScript to PDF Files in C# .NET

PDF is a rich document format in terms of versatility and features. One of the major advantages of PDF is its consistent layout across heterogeneous platforms. Moreover, it has the ability to be displayed in desktop applications and web browsers at the same time. This is the reason that PDF files are capable of running JavaScript. In this article, we will demonstrate how to add or remove JavaScript in PDF files using C#.

C# .NET API to Add or Remove JavaScript in PDF

Aspose.PDF for .NET is designed to perform PDF generation and manipulation within the .NET applications. The API lets you seamlessly create, process, and convert PDF files of simple and complex layouts. You can download the API’s binaries or install it using NuGet.

PM> Install-Package Aspose.PDF

Add JavaScript to PDF Files in C#

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 in C#.

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

Add Document Level JavaScript in a PDF in C#

You can also add JavaScript to the document level using Document.JavaScript property. The following are the steps to add JavaScript to the document level in C#.

The following code sample shows how to add JavaScript to a PDF in C#.

Remove JavaScript from a PDF in C#

The following are the steps to remove JavaScript from a PDF in C#.

The following code sample shows how to remove JavaScript from a PDF in C#.

Get a Free License

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

Conclusion

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

See Also