Parsing and rendering PDFs inside AIR with Flash

A while back i did a prototype that needed the functionality of parsing and rendering PDFs inside an AIR application, oh the joy…
Cleverly enough Adobe did include an HTMLLoader class with the AIR bundle, which also allows you to
load a PDF inside, just as it would in a normal browser. Tadaa! Simple aint it.

import flash.display.*;
import flash.events.*;
import flash.html.HTMLLoader;
import flash.net.URLRequest;
 
public class PDFParser extends MovieClip 
{
 
	private var pdf:HTMLLoader;
 
	public function PDFParser()
	{
		super();
	}
 
 
 
	public function createRender():void 
	{
		pdf = new HTMLLoader();
		pdf.width = this.width;
		pdf.height = this.height;
		addChild(pdf);
		pdf.addEventListener(Event.COMPLETE, handleCompleteEvent);
	}
 
	// Intercepts the HTMLLoader complete event and dispatches a new event since complete event. 
	//Does not bubble.
	private function handleCompleteEvent(event:Event):void 
	{
		var newEvent:Event = new Event(Event.COMPLETE);
		dispatchEvent(newEvent);
	}
 
 
	public function loadPDF( url:String ):void
	{
		var request:URLRequest = new URLRequest(url);
		pdf.load(request);
	}
}

By linking the class to a MovieClip in the library with an shape inside of it you can control the width and height of the PDF view without the need to pass args for width and height of the HTMLLoader instance.

Post a Comment

Your email is never shared. Required fields are marked *

*
*