Loading PDF files in Air with flex

Find how you can render PDF files in Air. This process is very simple and will be done in just a few minutes.

Download Source

Screenshot

1. First, create a new Air project named PdfReader that will also create an mxml file named PdfReader.mxml.
Open it so we can begin to edit our code.

2. Like for the WebBrowser we will use the HTML component.

3. The only thing that we have to do is to check if the user has a correct version of Adobe Reader installed because if the user doesn’t have Acrobat Reader 8.1 or later that will not work.

4. To do so, we have to detect the PDF capability with the static property pdfCapability of the HTML class.
The pdfCapability property can be one of the 5 values :

Screenshot

In our case we want to check the if the value is STATUS_OK

5. So in our code, add an HTML component to the application and create a function detectPDF that if the PDF capability is OK the nit will set the location of the HTML component to the path of the pdf file else it will call the Alert method to display an error message.

6. The whole code is as follow :

<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    creationComplete="detectPDF()" width="825" height="625">
    <mx:Script>
        <![CDATA[
        import mx.controls.Alert;
        
        private function detectPDF():void{
            if(HTML.pdfCapability== HTMLPDFCapability.STATUS_OK)
            {
            	reader.location = "assets/test.pdf";
            }
            else{
            	Alert.show("Can't display PDF content","Problem");
            }
        }
        ]]>
    </mx:Script>

    <mx:HTML id="reader" width="100%" height="100%"/>
</mx:WindowedApplication>

7. Run the application to test it.

9 thoughts on “Loading PDF files in Air with flex

  1. Thiago

    Great tutorial !! But I have a question.
    If I need that the application reads any pdf file inside that assets folder instead a specific name file.
    How I could do that ?

    I am waiting for a help.

    Thanks !!

  2. Abhishek

    Hi,
    Can you tell me please how to remove the toolbar or control the toolbar using flex.
    thanks and regards
    Abhishek

Leave a Reply

Your email address will not be published. Required fields are marked *