Sharing Script Libraries with Custom Commands

by Joel Geraci

posted on 03-09-2017

Joel Geraci has spent well over 30,000 hours working with Acrobat and PDF. With over 16 years at Adobe, he held a variety of roles including, Technical Evangelist, Business Development Manager, Product Manager, and Systems Engineer. Now, in his own PDF consultancy, he helps end users and developers build secure and reliable business workflows for PDF documents using technologies developed by Adobe.

JavaScript for Acrobat is secure, incredibly powerful, and enables you to automate a wide variety of tasks like sophisticated interactive forms. But the implementation of JavaScript for Acrobat and JavaScript for browsers is very different. Let’s learn how to navigate the differences and easily share script libraries using the Custom Command tool in Acrobat.

The ease with which feature-rich JavaScript libraries can be incorporated into HTML pages has helped drive the creation of community-based frameworks like jQuery, AngularJS, and Bootstrap to streamline development of highly interactive web pages. These frameworks build on the JavaScript Core to allow HTML to do things that it was never designed to do. Developers don’t even need to host these frameworks; one line of code in their HTML and everything just works better.

Adding JavaScript to PDF documents isn’t quite so simple. There are multiple dialogs, numerous script storage locations, and the developers who benefit the most from a JavaScript framework tend to be less experienced and less able to debug any mistakes made in storing the JavaScript. While there are several web sites devoted to sharing JavaScript for Acrobat, even simply copying code from one PDF and pasting it into another tends to be too much for many novice developers.

The Solution:

Custom Commands in Adobe Acrobat DC Pro changes all that. The Acrobat JavaScript API has methods to inject custom JavaScript in nearly every place that can be accessed by the user interface. These “script injectors” can be added to Custom Commands and then exported so they can be easily shared with others. The Custom Commands can then be imported by novice developers and incorporated into different workflows to easily add feature-rich libraries to any PDF file with the click of a button. Custom Commands are typically used to help end users with repetitive tasks. But, by leveraging Custom Commands, expert developers in the Acrobat Community can now easily share their code samples and libraries with everyone knowing they’ll be installed correctly the first time.

Creating a Custom Command Script Injector is a simple process that is described in the PDF linked at the end of this article, but first here are a few tips to ensure your new Custom Command works as intended.

Pro Tips:

  1. Ensure all string literals in your code are enclosed by either single quotes or double quotes – not a mixture. The methods used to add JavaScript to a PDF require that the script be represented as a string so you’ll need to wrap your code in quotes. A mixture of quote characters in your code will cause problems.
  2. Minify your code. This isn’t an absolute requirement but it will make your job easier; you won’t have to deal with line breaks or any quote characters inside of comments.
  3. Add an alert at the end of your Custom Command script that informs the user what your Custom Command did. The fact that some JavaScript got injected into the PDF won’t be obvious. Make it clear that your Custom Command actually ran properly.
  4. Come up with a naming scheme that will ensure your script doesn’t step on anyone else’s. JavaScripts added at the document level need to be named so try to select a name that will be unique to you.

For this example, the code that I want to run when my PDF documents first opens looks like this…
if ( typeof Field === 'function' ) { Field. prototype.isBoxChecked = function( index ) { if ( this.value == 'Off' ) { return false; } else { return true; } }; }
After minifying the code above, the JavaScript that will inject this code into the PDF Document looks like this…

var scriptName = "com.adobe.acrobat.FieldEx"; var scriptCode = "if(typeof Field==='function'){Field.prototype.isBoxChecked=function(index){if(this.value=='Off'){return!1}else{return!0}}}"; this.addScript( scriptName, scriptCode ); app.alert( "FieldEx script installed", 3 );
Once you have your JavaScript meeting the above criteria, follow the simple steps outlined in this sample file to create a Custom Command that can be used to distribute your JavaScript tools.

Download the sample file here and try it yourself!

Custom Commands in Acrobat is a powerful tool to automate the process of sharing JavaScript code with others. With this technique, you can easily share scripts with a broad audience and build a rich set of community-driven JavaScript libraries for Acrobat and Reader.


Products: Acrobat, Document Cloud