1. Home
  2. Foxit SDK .NET
  3. Working with JavaScript using Foxit PDF SDK (.NET)

Working with JavaScript using Foxit PDF SDK (.NET)

Foxit PDF SDK JavaScript is a language implemented within our library based on the core of JavaScript’s version 1.5. It is extremely useful for Web applications that need to use less memory from the server by offloading it onto the client. Foxit PDF SDK JavaScript implements extensions, in the form of new objects and their accompanying methods and properties, to the JavaScript language. Foxit’s library 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 Foxit-specific objects are added on top of core JavaScript, you still have access to its standard classes, including Math, String, Date, Array, and RegExp.

PDF documents have great versatility since they can be displayed both within the Foxit PDF SDK software as well as a Web browser. Therefore, it is important to understand two fundamental differences between Foxit PDF SDK JavaScript library and JavaScript used in a Web browser, also known as HTML JavaScript:

  • Foxit PDF SDK 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. Foxit PDF SDK JavaScript cannot access this particular object but it can manipulate PDF-specific objects.

Between many useful methods within PDF documents that Foxit PDF SDK JavaScript library enables, this article will highlight the ability to create JavaScript actions that occur within PDF’s objects such as annotations and form fields and more. These actions trigger a script to be compiled and executed by the JavaScript interpreter. The class foxit.pdf.actions.JavaScriptAction is derived from Action and offers functions to get/set JavaScript action data. These interactive objects allow great improvement of a document workflow such as filling out forms and submitting them via PDF files, manipulate annotation’s UI according to permission parameters and more, all of this with the possibility of attaching the functionality to enterprise workflows using the XML-based structure and support to webservices.

Below are examples on how to create JavaScript actions using Foxit PDF SDK in your PDF’s form fields and annotations:

Example:

How to add JavaScript Action to Document

using foxit.pdf.annots;
using foxit.pdf.actions;
 
// Load Document doc
...
using (JavaScriptAction javascipt_action = new JavaScriptAction(action))
{
 javascipt_action.SetScript("app.alert(\"Hello Foxit \");");
 using (AdditionalAction aa = new AdditionalAction(doc))
 {
 aa.SetAction(AdditionalAction.TriggerEvent.e_TriggerDocWillClose, javascipt_action);
 }
}
...

How to add JavaScript Action to Annotation

using foxit.pdf.annots;
using foxit.pdf.actions;
...
 
// Load Document and get a widget annotation.
...
using (JavaScriptAction javascipt_action = new JavaScriptAction(action))
{
 javascipt_action.SetScript("app.alert(\"Hello Foxit \");");
 using (AdditionalAction aa = new AdditionalAction(annot))
 {
 aa.SetAction(AdditionalAction.TriggerEvent.e_TriggerAnnotMouseButtonPressed, javascipt_action);
 }
}
...

How to add JavaScript Action to FormField

using foxit.pdf.interform;
using foxit.pdf.actions;
...
 
// Load Document and get a form field.
...
 
using (JavaScriptAction javascipt_action = new JavaScriptAction(action))
{
 javascipt_action.SetScript("AFSimple_Calculate(\"SUM\", new Array (\"Text Field0\", \"Text Field1\"));");
 using (AdditionalAction aa = new AdditionalAction(field))
 {
 aa.SetAction(AdditionalAction.TriggerEvent.e_TriggerFieldRecalculateValue, javascipt_action);
 }
}
...
Updated on October 23, 2019

Was this article helpful?

Related Articles