Handling Acrobat JavaScript on a Server with the Datalogics PDF Java Toolkit

Handling Acrobat JavaScript on a Server with the Datalogics PDF Java Toolkit

http://www.dreamstime.com/stock-photo-javascript-concept-blue-background-blue-text-image44066030

Sample of the Week:

Joel GeraciJavaScript is generally associated with scripting HTML in a browser but JavaScript can also be used to automate Adobe Acrobat, Reader, and the PDF files viewed with them. The viewers and their plug-ins expose a lot of their functionality as JavaScript objects; generally referred to as “Acrobat JavaScript.”  Acrobat JavaScript lets developers automate workflows through Actions as well as program PDF Forms to help guide the recipient through the filling or signing process. Acrobat JavaScript is incredibly powerful and can make your documents so interactive that they appear to function as applications themselves.

Many PDF form developers create their forms with the assumption that they will be consumed in an Adobe viewer. However, more and more, this is less and less true. The leading browsers and operating systems now come with their own PDF viewing technology and they all handle Acrobat JavaScript differently… some… not at all. When a PDF that contains Acrobat JavaScript is opened in one of these applications, it’s generally ignored but often simply fail. I’m not sure which is worse but in both cases, the document isn’t being viewed as the author intended.

Unfortunately, the situation isn’t much better when it comes to PDF developer tools. The PDF libraries that are primarily used for PDF creation can inject Acrobat JavaScript into form fields and at the document level but most of these libraries, including the Adobe PDF Library, can’t consume Acrobat JavaScript.

However, because the Datalogics PDF Java Toolkit was developed by Adobe as the heart of LiveCycle, it can process Acrobat JavaScript in much the same way that Acrobat does. The JavaScriptHandler class can be used to execute JavaScript associated with the event object passed to it; a field calculate or format event for example.

The Datalogics PDF Java Toolkit even has a way to proxy the Adobe viewers so that your document behaves as though it’s being viewed in Acrobat or Reader; you can even tell it what version you want to impersonate and what plug-ins are installed. The PDF Java Toolkit JSExtension sample shows how to use the JavaScriptHandler and JSAppProvider classes to replace an object that is present in Acrobat wit hone that is functionally equivalent.

Code Snippet:

/*
 * The sample implementation of ExtensionApp interface that provides
 * extension support to allow PDF JT to proxy the Acrobat JavaScript App
 * properties and methods.
 */
class JSAppProviderImpl implements JSAppProvider{
public double viewerVersion() {return 11.004;}
	public String viewerVariation() {return "Full";}
	public double formsVersion() {return 11.004;}
	
	// The following will make form calculations run by PDF JT to respond as
	// though the digsig and ppklite plugs in for Acrobat XI are available. 
	public int numPlugIns() {return 2;}
	public List plugIns() {
		List plugInList = new ArrayList();
		plugInList.add(new digSig());
		plugInList.add(new ppklite());
		return plugInList;
	}

}
	
/*
 * Proxies for the digsig and ppklite plugins in Acrobat XI 
 */
class digSig implements JSPlugInProvider{
	public boolean certified() {return true;}
	public String path() {return "C:\\Program Files (x86)\\Adobe\\Acrobat 11.0\\Acrobat\\plug_ins\\DigSig.api";}
	public double version() {return 11;}
	public String name() {return "DIGSIG";}
	public boolean loaded() {return true;}
}

class ppklite implements JSPlugInProvider{
	public boolean certified() {return true;}
	public String path() {return "C:\\Program Files (x86)\\Adobe\\Acrobat 11.0\\Acrobat\\plug_ins\\PPKLite.api";}
	public double version() {return 11;}
	public String name() {return "ppklite";}
	public boolean loaded() {return true;}
}

By leveraging the JavaScript related classes in the Datalogics PDF Java Toolkit, developers can ensure that interactive PDF documents are processed on their servers in the same way that they are processed by Acrobat.

View and download the JSExtension sample or get all the samples and documentation by requesting an evaluation of the Datalogics PDF Java Toolkit.

Leave a Reply

Your email address will not be published. Required fields are marked *