Working with JavaScript using Foxit PDF SDK (Objective-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. Class ‘FSJavaScriptAction‘ is derived from FSAction 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:
Contents
How to add JavaScript Action to Document
#include "FSPDFObjC.h" ... // Load Document doc. ... FSJavaScriptAction *javascript_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]]; javascipt_action.script = @"app.alert(\"Hello Foxit \");"; FSAdditionalAction *additional_act = [[FSAdditionalAction alloc] initWithDoc:doc pdf_dict:nil]; [additional_act setAction:FSAdditionalActionTriggerDocWillClose action:javascipt_action]; ...
How to add JavaScript Action to Annotation
#include "FSPDFObjC.h" ... // Load Document and get a widget annotation. ... FSJavaScriptAction *javascript_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]]; javascipt_action.script = @"app.alert(\"Hello Foxit \");"; FSAdditionalAction *additional_act = [[FSAdditionalAction alloc] initWithAnnot:widget_annot]; [additional_act setAction:FSAdditionalActionTriggerAnnotMouseButtonPressed action:javascipt_action]; ...
How to add JavaScript Action to FormField
#include "FSPDFObjC.h" ... // Load Document and get a form field. ... FSJavaScriptAction *javascript_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]]; javascipt_action.script = @"AFSimple_Calculate(\"SUM\", new Array (\"Text Field0\", \"Text Field1\"));"; FSAdditionalAction *additional_act = [[FSAdditionalAction alloc] initWithField:field]; [additional_act setAction:FSAdditionalActionTriggerFieldRecalculateValue action:javascipt_action]; ...
Updated on March 26, 2019