Printing in AIR 2

I recently had the opportunity to modify an application that Rick Rocheleau wrote to demonstrate the printing improvements in AIR 2. With the AIR 2 Release Candidate publicly available, it seems like a good time to let developers experiment with the new and improved APIs. I’m releasing the source code for a version of Rick’s app that I modified to use Flex 4, which also includes some helpful comments for developers.

Here’s a screencast that summarizes the application and some of the AIR printing APIs:

What I demonstrate in the video–and what you can try for yourself by downloading the code and/or AIR file–is a Flex 4 (Spark) group that lays out text in a printable area; this area is constrained by the paper selection, and will print to any attached printer exactly as it appears on the screen. The printing APIs in AIR 2 let you hand it any sprite, which will get passed along to the system in either vector (PrintMethod.VECTOR) or bitmap (PrintMethod.BITMAP) modes. AIR 2 has increased performance for both these modes, and also adds vector printing support on Mac. There’s also a new printing mode called auto (PrintMethod.AUTO) that will leave the mode decision up to the runtime; this has the advantage of not requiring you to guess which mode is best for printing the user’s content. In auto mode, the runtime will choose between vector or bitmap mode for performance and speed. In this application the appearance of a translucent watermark on the content will cause the runtime to choose bitmap mode, and its absence will result in it choosing vector mode.

Another notable improvement is the ability to get a list of printers from the system without having to start a PrintJob first; you can use this information to create a drop down box of choices for the user, and set which printer should be used for future jobs. You can also set the paper size, change orientation, receive information about the printable area based on your paper selection, and gain more control over the printing UI. You can choose to display the page setup dialog (without needing to start a PrintJob), and you can also choose to print by displaying the native system UI or suppress the native UI and send the job directly to the printer. The latter of those two options is useful if you want your application to control all aspects of the page/printer setup, and/or if you want to streamline the printing process for the user.

I encourage you to watch the screencast to learn more, and to check out the source code and application:

Source code: PrintFeatureDemo.zip

AIR file: PrintFeatureDemo.air

You’ll need a version of AIR 2 to both compile and run this print demo. You can use the publicly available AIR 2 Release Candidate:

AIR 2 Release Candidate on Adobe Labs: http://labs.adobe.com/technologies/air2/

AIR 2 will give you more control and performance with printing; AIR 2 is not yet a final release, and this printing app is just a demo. If you believe you’ve found bugs in either, don’t hesitate to report them here. I’ll make sure they’re addressed. I’m also eager to hear about your ideas or improvements for the printing app.

Update 1: I uploaded new versions of the AIR file and source code, which should fix a problem with running or compiling the code using AIR 2 Beta 2.

Update 2: The documentation for AIR 2 Beta 2 is available through Adobe Labs, but here’s the direct link to the Beta ActionScript 3 docs: http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/index.html

Update 3: We just released an AIR 2 Release Candidate (May 11th, 2010), and I’ve updated the post to point you to this version. I’ve also updated the AIR file and code, so that it’ll work with AIR 2 RC (and not AIR 2 Beta 2).