Adobe Consulting Public Sector: Acrobat Archives

June 26, 2008

Acrobat 9 is shipping and Here is how to communicate between PDF and Flash/Flex

Prior Acrobat 9, we were able to communicate between a PDF and a Flash/Flex application residing in the same browser window (e.g. the iframe approach). Acrobat 9 introduces a new feature called Rich Media Annotation that allows you to amazing stuff with Flash ... one of them being the ability to embed Flash into a PDF and communicate with it via JavaScript. The example here is simple PDF document with an embedded Flex application that takes a product ID from the PDF and queries Amazon Web Services for product information (features, price, release date etc.).

In addition to the Flash playback support, Acrobat/Reader 9 can communicate with embedded SWF by utilizing the ExternalInterface API. This means that you must register your ActionScript function to indicate that it should be made available to the PDF by using the addCallBack() method as follows:


/* ActionScript */
// Register an ActionScript method as callable from the container
ExternalInterface.addCallback("find", find);

// Find a product
public function find(itemId:String):void
{
	...
}

On the PDF side, all you need to do is to get hold of the Flash object and call into the ActionScript method by using the callAS( methodName:String, <argument1, argument2, and so on...>) as follows:


/* JavaScript */

// Get hold of the first Flash object on the first page
var annot = getAnnotsRichMedia(0)[0];

if ( annot ) 
{
	// if exists, call the function called "find"
	// and pass an item ID as an argument	
	annot.callAS( "find", "B000NDIB6Y");
}

Again, what the callAS method does is calling into ActionScript via ExternalInterface calling convention to an exposed method. The example I mentioned in this post can be downloaded here. Acrobat/Reader 9 is required. If you want to reuse the Flex application, you will need to register for an Amazon Web Services and their Associates accounts.

Downloads

How-to Steps:

  1. Create a blank PDF or open an existing one in Acrobat 9.
  2. Add a button by going into the form editing mode.

    Form Editing

  3. Add a Run Javascript action to the button.

    Run JavaScript Action

  4. Add the Flex SWF to the PDf by using the Flash tool.

    Flash tool

  5. Editing the Flash properties to make it start as soon as the PDF is opened.

    Flash properties

  6. Save the PDF and the end result should look similar to this.

    Screenshot

Posted by Mick Lerlop (lerlop) at 11:17 AM | Comments (0) | TrackBack

May 15, 2008

Using Image in Acrobat JavaScript Dialog

I have had customers asking about how to brand Acrobat JavaScript dialog boxes with images in Adobe LiveCycle Designer. I did some research and found that it was not as simple as defining an <img href=”image.png”/> tag (I wish it was that easy though). As an overview, images used in a Acrobat JavaScript dialog has to be in a icon stream format represented by a hex-encoded string. The data string also needs to be 32 bits per pixel with 4 channels (ARGB) or 8 bits per channel with the channels interleaved. The hex-encoded string looks something like this, "fffffffffff...efdf8fff0e3beffd3b". Beautiful. Anyway, moving on.

There are a number of free and commercial third party tools you could use to convert images to hex-encoded strings. However, none of the tools fits my workflow in terms of flexibility and extensibility. So I have created a Java utility library, called Acrobat Dialog Image Generator (ADIG), which allows you to generate a hex-encoded string or a skeleton Acrobat dialog box with an embedded image.

You can invoke ADIG via a command-line interface, ANT or an API call. Here are some sample invocations:

Command-line

java -jar adig.jar /Users/lerlop/Pictures/test.jpg /Users/lerlop/Desktop/

This will reads in test.jpg and generates a JavaScript dialog file called test.jpg.txt on my desktop. Here is a sample.

ANT

ant -buildfile adig.xml

This will also produce the same result as the command-line option but everything is defined in the following build file.

    
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="GeneratorAcrobatDialogImage" basedir=".">
	<taskdef
		name="adig"
		classname="com.adobe.consulting.ant.tasks.GenerateAcrobatDialogImage"
		classpath="../../build/adig.jar"/>

	<target name="GeneratorAcrobatDialogImage">
		<adig imagePath="/Users/lerlop/Pictures/test.jpg" outputPath="/Users/lerlop/Desktop"/>
	</target>
</project>
    

API Call

String hexString = AcrobatDialogImageGenerator. generateHexEncodedString(imagePath);

By calling this static method in your Java code, you can generate a hex-encoded string from an image and assign it to a String object. This particular option is very useful when you try to perform any kind of batch operations. For instance, I can call this method to dynamically insert an image to a JavaScript dialog defined in an XDP before calling LiveCycle Forms to render it as a PDF.

To use the generated dialog JavaScript code in your form design, you can follow these simple steps:

  1. Run ADIG via command-line or ANT to get a generated file. The generate code should like this.
  2. Open your form in LiveCycle Designer (note: XFA form not AcroForm).
  3. Create and name a new script object. It does not matter where the script object is located as long as you can reference it later. For simplicity, create it on page 1 and let’s call it DialogSO.
  4. Copy all the code from the generated file and paste it into the script object.
  5. Now create a button object so you can use it to launch the dialog box. Note that you could launch the dialog box in any event such as form::docReady or form::initialize.
  6. The last thing is to make the button launch the dialog box. In the click event of the button, type in the following function call: DialogSO.launchDialog();

There you have it. You can extend the dialog box in any way you like by adding dialog elements to the dialog body. Please refer to the JavaScript for Acrobat API Reference.

I want to note that there are commercial tools out there that will let you design and extend Acrobat dialog boxes far more than just adding an image and generating dialog JavaScript template. If you are looking for a WYSIWYG tool to design dialog boxes, WindJack’s AcroDialogs may be more suitable for your needs.

Downloads:

Posted by Mick Lerlop (lerlop) at 04:33 AM | Comments (1) | TrackBack