Foxit PDF SDK for Windows

Working with JavaScript using Foxit PDF SDK (C++)

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 com.foxit.sdk.pdf.actions.JavaScriptAction is derived from the PDF class Action (foxit::pdf::actions::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

#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"

using namespace foxit;
using namespace pdf;
using namespace annots;

// Load Document doc.
...

actions::JavaScriptAction javascript_action = (actions::JavaScriptAction)Action::Create(form.GetDocument(), Action::e_TypeJavaScript);
javascipt_action.SetScript(L"app.alert(\"Hello Foxit \");");
AdditionalAction additional_act(doc);
additional_act.SetAction(AdditionalAction::e_TriggerDocWillClose,javascipt_action);
...

How to add JavaScript Action to Annotation

#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"

using namespace foxit;
using namespace pdf;
using namespace annots;

// Load Document and get a widget annotation.
...

actions::JavaScriptAction javascript_action = (actions::JavaScriptAction)Action::Create(form.GetDocument(), Action::e_TypeJavaScript);
javascipt_action.SetScript(L"app.alert(\"Hello Foxit \");");
AdditionalAction additional_act(annot);
additional_act.SetAction(AdditionalAction::e_TriggerAnnotMouseButtonPressed,javascipt_action);
...

How to add JavaScript Action to FormField

#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"

using namespace foxit;
using namespace pdf;
using namespace annots;

// Load Document and get a form field.
...

actions::JavaScriptAction javascript_action = (actions::JavaScriptAction)Action::Create(form.GetDocument(), Action::e_TypeJavaScript);
javascipt_action.SetScript(L"AFSimple_Calculate(\"SUM\", new Array (\"Text Field0\", \"Text Field1\"));" );
AdditionalAction additional_act(field);
additional_act.SetAction(AdditionalAction::e_TriggerFieldRecalculateValue,javascipt_action);
...

How to add a new freetext Annotation to PDF using JavaScript

#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"

using namespace foxit;
using namespace pdf;
using namespace annots;

// Load a document
PDFDoc doc = PDFDoc(input_file);
doc.Load();
// Prepare Additional Action 
AdditionalAction pAAction(doc);
if (pAAction.IsEmpty()) {
return err_ret;
}

// Create new Action of JavaScript type
Action new_action = Action::Create(doc, Action::e_TypeJavaScript);
if (new_action.IsEmpty()) {
return err_ret;
}

// Assign JavaScript action to new action
JavaScriptAction js_action = (JavaScriptAction)new_action;
if (js_action.IsEmpty()) {
return err_ret;
}

// Define the script to be used by the JavaScript action
js_action.SetScript(L"var annot = this.addAnnot({ page: 0, type: \"Text\", point: [72, 30], popupRect: [72, 300, 3 * 72, 300 - 2 * 72], popupOpen: true, noteIcon: \"Help\"});");
// Set the JavaScriptAction and the trigger event to the Action
pAAction.SetAction(AdditionalAction::e_TriggerDocWillClose, js_action);
// Execute the JavaScript action when the event is triggered on runtime
pAAction.DoJSAction(AdditionalAction::e_TriggerDocWillClose);
// Save PDF file
WString newPdf = output_directory + L"js_annotation_3.pdf";
doc.SaveAs(newPdf, PDFDoc::e_SaveFlagNoOriginal);

 

Updated on March 25, 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: