Learning Flex 4 | O'Reilly Media

Load PDF Content into an AIR Application

with 6 comments

It’s deceptively simple to load a PDF file into an AIR appliction. Basically, point an HTMLLoader at a URLRequest—which can take a local file path—add the HTMLLoader to a UIComponent, then add the UIComponent to a container.

In this case, I added the UIComponent directly to the application, but it stands to reason you could put it in any container.

Flex 4 / AIR Application Code

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication title="PDF Viewer"
      xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx"
      height="600" width="525"
      backgroundColor="#000000">

<s:Button label="Load The PDF" click="loadPdf(path)" top="4" left="4"/>

<s:Button label="Close The PDF" click="closePdf()" top="4" right="4"/>

<fx:Script>
<![CDATA[
  import mx.core.FlexGlobals;
  import mx.core.UIComponent;

  private var pdfUIC:UIComponent;

  // double forward-slashes necessary for win file paths..
  private var path:String = "C:\\pdf_files\\original\\filename.pdf";

  private function loadPdf(fullPathToPDF:String):void
  {
    var pdfRequest:URLRequest = new URLRequest(fullPathToPDF);
    var pdfHTMLLoader:HTMLLoader = new HTMLLoader();

    // 1) load the PDF into an HTMLLoader (Air-only)
    pdfHTMLLoader.height = FlexGlobals.topLevelApplication.height-70;
    pdfHTMLLoader.width = FlexGlobals.topLevelApplication.width-60;
    pdfHTMLLoader.load(pdfRequest);

    // 2) add the HTMLLoader to a UIComponent
    pdfUIC = new UIComponent();
    pdfUIC.addChild(pdfHTMLLoader);
    pdfUIC.name = "pdfPrintPreview";

    // 3) add the UIComponent to the application
    pdfUIC.y = 33;
    pdfUIC.x = (FlexGlobals.topLevelApplication.width - pdfHTMLLoader.width) / 2;
    this.addElement(pdfUIC);
  }

  // destroy the PDF, call garbage collector..
  private function closePdf():void
  {
    if(pdfUIC)
    {
      this.removeElement(pdfUIC);
      
      // make the object a candidate for garbage collection..
      pdfUIC = null;
      
      flash.system.System.gc();
    }
  }

]]>
</fx:Script>

</s:WindowedApplication>

Lo! The sample application running in Debug mode..

Load a PDF into Adobe AIR Using Flex 4

Load a PDF into Adobe AIR Using Flex 4

Advertisements

Written by elrobis

2 July 2011 at 6:41 am

Posted in ActionScript 3, AIR, Flex 4

6 Responses

Subscribe to comments with RSS.

  1. it is not releasing memory when i press close button.

    Anonymous

    20 December 2011 at 1:07 am

    • Hmm.. yes that makes sense. Try setting pdfUIC = null in the closePdf() function, like so:


      // destroy the PDF, call garbage collector..
      private function closePdf():void
      {
      if(pdfUIC)
      {
      this.removeElement(pdfUIC);

      // make the PDF a candidate for GC
      pdfUIC = null;

      flash.system.System.gc();
      }
      }

      Setting the reference to null prior to calling the garbage collector should make the object a candidate for GC. I don’t have FB Pro so I can’t be 100% sure this solves the problem; however, I believe it’s theoretically correct. I updated the post according to your observation, and I apologize for the oversight. Please let me know if it doesn’t work.

      /Elijah

      elrobis

      21 December 2011 at 6:16 pm

      • Is there a way to make the pdf viewer not have any pre-existing controls, and just create some custom ones, even better add pinch zooming?

        alastair reece

        12 April 2013 at 6:22 pm

      • Hi Alastair, unfortunately I never experimented with hiding, moving, or extending the built-in tools.

        However you might want to check out the following example. It seems they loaded the PDF differently, in a manner that avoids the built-in controls. I suspect this is more along the lines of what you want to accomplish:

        http://blogs.adobe.com/digitalmedia/2011/02/how-to-work-with-pdf-in-adobe-air-source-code/

        In my own experience (maybe even 100% of production implementations), when I want to access a PDF from an application, I just invoke a URL to the PDF in a new window and let the user have their way with it from there. :) From AS3 it’s like this:

        getURL( fullPathToPDF, “_blank” );

        Anyway I hope something there is helpful for you.

        Best / Elijah

        > Date: Fri, 12 Apr 2013 23:22:14 +0000 > To: soloorbit@hotmail.com >

        elrobis

        13 April 2013 at 11:24 am

  2. hi mine will not display..

    dark

    28 May 2012 at 12:10 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: