Avg. Rating 3.4

Problem

I'd like to show a PDF inside my AIR application, but Flex does not provide a PDF display component.

Solution

While AIR does not provide a feature to display a PDF directly, it is simple to present a PDF through Flex' mx:HTML component. This entry wraps mx:HTML into a single window to show a PDF.

Detailed explanation

To present a preview window, you need to create an instance of CPDFPreview window first:

  var aPreview:CPDFPreview = new CPDFPreview();
  aPreview.open( true );

Then pass a file reference to loadFile(), which points to the PDF

  aPreview.loadFile( fp );

or pass an URL string directly

  aPreview.loadFile( "file://" + fp.nativePath  );

loadFile() provides a second parameter which default to FALSE. In case you pass TRUE, CPDFPreview displays the PDFs URL-location in the windows status area:

 

Here is the simple component CPDFPreview, which wraps an mx:HTML component inside a window in such a way, that it fills the whole window.

In fact, the component visualizes any kind of stuff, the HTML component is able to visualize. I called it CPDFPreview, because Adobe might at some day in the future PDF feature and - in my case - I present PDFs created using AlivePDF.

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Window xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="912" height="636"
    creationComplete="init();"
    >

    <mx:HTML id="htMainView" right="0" left="0" top="0" bottom="0"/>
   
    <mx:Script>
        <![CDATA[
       
            private const  DEFAULT_TITLE:String    = "PDF Preview";
               
            private function init():void {
            }
           
            public function loadURL( anURL:String = "http://your.url.default/", showStatusLocation:Boolean = false ):void {
           
                title = DEFAULT_TITLE;
                if ( true == showStatusLocation ) {
                    status = ' Preview of: ' + anURL;
                }
                htMainView.location = anURL;
               
            }

            public function loadFile( anURL:File, showStatusLocation:Boolean = false  ):void {
           
                if ( null != anURL ) {
                    loadURL( "file://" + anURL.nativePath, showStatusLocation );
                }
               
            }
           
        ]]>
    </mx:Script>
</mx:Window>

Report abuse

Related recipes