QueTwo's Blog

thouoghts on telecommunications, programming, education and technology

Printing in Flex (Part 2)

So, now I’ve confused you all on how to use the DataGrid component to print, lets talk about printing in Flex.

There are two theories in printing in Flex. One is what I call the "Clean-Sheet" method, where you create a new Flex Component to represent the page you will be physically printing to. The Clean-Sheet method allows the greatest amount of control (you can set things like headers/footers, margins, gutters, etc.), but it is the most difficult and most verbose to program for.

The other method is the just add existing items to the print-queue. This requires very little code, and is still pretty accurate. I’ll begin with some code and show you what each line does.

import mx.printing.FlexPrintJob;
public function printGraph():void
var printJob:FlexPrintJob = new FlexPrintJob();

Hopefully, that didn’t look too confusing… Anyway, so what is happening is first, I’m setting up a script block to do some ActionScript (and including the CDATA block so I can write some inline ActionScript code within the MXML. I then import the mx.printing.FlexPrintJob library to facilitate the printing.

Once I’ve setup the enviroment, I then setup a function that will be called to print an object. In my case, I setup a button (not shown) that has a click event that is like click="printGraph();".

I then setup a new in-memory object to hold my printed page. This new object is named printJob and has a type of FlexPrintJob (which is what we just imported).

We then setup a new print queue. I do this in an if statement, by issuing the command: printJob.start() This will also launch the Windows "Select A Printer" dialog that users are familar with. You will now need to add objects to the print queue. Each object you add represents ONE PAGE in the print. Keep this in mind when you are adding objects, and pick carefully which objects you want to add (Hint: add a canvas or an HBox instead of that label). You add objects to the queue by accessing the printJob’s function addObject, and then passing the object you wish to add. In the example’s case, there exists an object in the local scope named myChart.

Next, you will need to send the object to the printer. The print will NOT begin until you call the send() function of the printJob object. The user will not gain control of your application until this function has been called.

That’s pretty much it to do a simple print. Remember, if your object called for 100% width of its parent, then the object will occupy 100% of the available space on the page.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: