Skip navigation.
Home

How to print in Flex 2.01

This is just MY way to print in Flex. There might be a better way, but I have not found it :-) If anyone knows of one, please share it with me! First off there are two REQUIRED classes that you will need to include.

I am guessing no matter how you print you will need these.

1. FlexPrintJob: This class allows you to print one or more objects.  The objects can be containers or custom compnents that are built just for displaying printed material.  Personally I like the custom component for printing. I think it breaks things up code wise easier. 

This class automaticall splits large jobs over multiple pages for you and will scale to fir your desired paper size.

2.  PrintDataGrid:  This a subclass of the DataGrid COntrol.  This class allows for better control over the apperance of datagrid pritning.  

 This is how you import the needed classes:

<mx:Script>
    <![CDATA[

        import mx.printing.FlexPrintJob;
       import mx.printing.PrintDataGrid;

       
    ]]>
</mx:Script>

 ---------------------------------------------------------------------

Function Making time  All this will be done in the script block

----------------------------------------------------------------------

private function doPrint():Void

{

    var pj:FlexPrintJob = newFlexPrintJob();

 } 

 --------- ----------------------------------------------------------------------------------
We just created our firs t variable that revolves around the function doPrint(). 
The name is 'pj' 
The FlexPrintJob  class is were everything will be revolved around
---------------------------------------------------------------------------------------------

private function doPrint():Void

{

    var pj:FlexPrintJob = newFlexPrintJob();
    pj.start();

 }

---------------------------------------------------------------------------------
We now want to invoke the start() method on pj:FlexPrintJob
This starts the FlexPrintJob object and causes the OS to display the
'Print Dialog'
---------------------------------------------------------------------------------

 --------------------------------------------------------------------------------
Now this might get a bit confusing.  I will try to explain this so you do not
get confused or frustrated. 

1.  on the pj object we will invoke addObject(); method 
2.  We will pass the ID of the component that we want to PRINT. 
3.  Lets say we are PRINTING a VBOX with an ID of printContainer

Soooo the method will look like this:

pj.addObject(printContainer); 

Now for you application PLEASE replace the 'printContainer' ID with the ID of your component
that you want to print.

 At this point our function should look like this:

private function doPrint():Void

{

    var pj:FlexPrintJob = newFlexPrintJob();
    pj.start();
    pj.addObject(printContainer);

 }

-----------------------------------------------------------------------------------------
The next method is almost self eplanatory
pj.send();
This method sends the object to the printer -----------------------------------------------------------------------------------------

private function doPrint():Void

{

    var pj:FlexPrintJob = newFlexPrintJob();
    pj.start();
    pj.addObject(printContainer);
    pj.send();

 }

---------------------------------------------------------------------------------------
Now just link thefunction to your button.  Simple enough huh ;-)
---------------------------------------------------------------------------------------