Printing in Flex

I had to print something from Flex, but the LiveDocs weren’t that helpful. I found this function and modified it to make it more generic…

<mx:Script>
  <![CDATA[
    import mx.core.IUIComponent;
    import mx.printing.FlexPrintJob;
    private function doPrint(printMe:IUIComponent):void{
      var pj:FlexPrintJob = new FlexPrintJob();
      if( pj.start() != true ){
        return;
      }
      pj.addObject(printMe);
      pj.send();
    }
  ]]>
</mx:Script>

Then define some display object you’ll want to print. I loaded another component into this canvas.

<mx:Canvas
id="formContent" backgroundColor="#ffffff"/>

And a button to print, passing it the component above. It’s that simple!

<mx:Button label="Print" click="doPrint(formContent)"/>

~ by codingmurloc on May 11, 2007.

Leave a Reply

You must be logged in to post a comment.