JavaScript controlled checks - referencing other checks
In some cases, checks can reference other checks. This is for example the case when Sifter (context-aware preflight) is used, or if results of other checks are included in a check. An example of the latter is the following check:
In the context of the "Arbitrary JavaScript controlled Checks" condition, this raises the question how such a check referencing one or more other checks can be represented. There are two ways this can be handled in the JavaScript.
Using resources
First of all, the "Resources" section can be used.
The "Resources" tab is the second tab of the "Arbitrary JavaScript controlled Checks" condition (the first tab is where you write the JavaScript, as discussed in previous articles). This tab contains a JSON object, with a fixed structure as shown in the screengrab above. The object contains a property called "checks", whose value is an array of names of referenced checks.
You can build this array in three different ways:
- You can add more lines to the array, and then make sure the name of an existing check is added to the array.
- You can use the 'i' button, and click the "Insert custom check..." button. After selecting the check you want, it's name is added to the array. If the check you add itself references other checks, those are added to the array as well.
- You can go to the "Script" tab and insert a custom check. If the check you insert contains referenced checks, these are added to the array in the "Resources" tab automatically.
Why adding resources is important
When pdfToolbox exports checks (or anything else) into a kfpx file, it normally embeds all used resources into the kfpx so that they are automatically transferred to the target machine. When using JavaScript to define checks, pdfToolbox cannot always determine which additional checks may be referenced from your code (as you can literally build the name of the checks you need on the fly).
To solve this, this "Resources" section exists. Any check you add to the resources is automatically exported to the kfpx file to make it available anywhere your check is imported.
Using JavaScript
While using resources to re-use already defined checks is certainly easy, it might not suffice for every use case. You might have to build the referenced checks on the fly, just as the main check. In that case, you can define the check using JavaScript.
The screengrab shows the main "checks" array for an "Arbitrary JavaScript controlled Checks" condition. It features two conditions.
The first condition references a check from the "Resources" tab. As you can see, it references the check by name. The second condition doesn't do this, but has a JavaScript object that defines the check. The structure of this JavaScript is exactly the same as the JavaScript for the main check and you can have pdfToolbox create it for you using the "Insert base check..." or "Insert configured check..." menu items.