Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

Working with JavaScript using Foxit PDF SDK (Java)

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. Class com.foxit.sdk.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

import com.foxit.sdk.pdf.actions.Action;
import com.foxit.sdk.pdf.actions.JavaScriptAction;
...
// Load Document doc
...
JavaScriptAction javascipt_action = new JavaScriptAction(Action.create(form.getDocument(), Action.e_TypeJavaScript));
javascipt_action.setScript("app.alert(\"Hello Foxit \");");
AdditionalAction aa = new AdditionalAction(doc);
aa.setAction(AdditionalAction.e_TriggerDocWillClose, javascipt_action);
...

How to add JavaScript Action to Annotation

import com.foxit.sdk.pdf.actions.Action;
import com.foxit.sdk.pdf.actions.JavaScriptAction;
...
// Load Document and get a widget annotation.
...
JavaScriptAction javascipt_action = new JavaScriptAction(Action.create(form.getDocument(), Action.e_TypeJavaScript));
javascipt_action.setScript("app.alert(\"Hello Foxit \");");
AdditionalAction aa = new AdditionalAction(annot);
aa.setAction(AdditionalAction.e_TriggerAnnotMouseButtonPressed, javascipt_action);
...

How to add JavaScript Action to FormField

import com.foxit.sdk.pdf.actions.Action;
import com.foxit.sdk.pdf.actions.JavaScriptAction;
...
// Load Document and get a form field
...
JavaScriptAction javascipt_action = new JavaScriptAction(Action.create(form.getDocument(), Action.e_TypeJavaScript));
javascipt_action.setScript("AFSimple_Calculate(\"SUM\", new Array (\"Text Field0\", \"Text Field1\"));");
AdditionalAction aa = new AdditionalAction(field);
aa.setAction(AdditionalAction.e_TriggerFieldRecalculateValue, javascipt_action);
...

Updated on March 26, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: