Viewing PDFs with AIR


One of the nice things AIR allows is the ability to view both HTML and PDF content. Here’s a little app that shows how you can view PDF documents on the local computer.

The key is the flash.html.HTMLLoader class. This is a FLASH class and cannot be directly used with Flex components as it extends Sprite and does not implement the Flex IUIComponent interface.

This is easy to remedy though. Just create a new class that extends UIComponent (I called mine PDFViewer) and include an HTMLLoader member. Override createChildren() to make a new HTMLLoader instance and add it to the UIComponent’s display list. Then override updateDisplayList to size and position it.

The main AIR application uses a FileSystemTree to allow the user to navigate to their PDF files and select one. The file’s URL is passed to the PDFViewer which then loads it and displays it.

And that’s pretty much it. There are some caveats to use PDF with AIR which you can read in the documentation, but if you want to use PDFs for help/documentation on your product or to display a PDF generated by a server (eg, ColdFusion), it is pretty easy to do.

Download Example Code

Comments are closed.