Print Job in flex 2 beta 1

I had several tips for working with print job in flex 1.5 ( See Tips for using PrintingJob in flex 1.5). Now that the flex 2 beta is available to the public, I am trying to convert the examples to run with flex 2. The migration does not take a lot of work. However, there are some glitches. I am trying to document as much as I could to make the conversion easier for others. Needless to say, we are working with beta 1. Therefore, any thing can be changed in the future release. This is not intent to show how print job will work in flex 2. It is just an exercise to prepare myself for the final migration.

Before we start to talk about conversion for each example, we need to start with the general migration steps. Please see ActionScript 3 Learning Tips. For our code, the following are most important:

Declare types for all variables, parameters and return values.
For example: for (var i=0;i<pages;i++) should be for (var i:int=0;i<pages;i++)

Declarations with no access specifier now default to package internal, not public.
function doPrint(){} should be: public function doPrint(){}

Classes must be imported, even if references to the class are fully qualified.
Do not forget to do import mx.print.FlexPrintJob;

Properties are no longer bindable by default.
You must declare them to be bindable by using the [Bindable] metadata tag.

Functions must declare return types.
If a function does not return any value, declare its return type as void. Note,
although the document all indicating it should be “Void”, it must be “void” with
lower case.

The next important thing to note is that flex 2 uses FlexPrintJob instead of PrintJob class. Also, it is using addObject() instead of addPage() method.

The above mentioned items are general for all the print examples and should be modified accordingly in each file.

1. Migrating printDataGrid.mxml from flex 1.5:

In flex 1.5, we handle the multiple pages by calculating the page numbers we needed to print out all the rows in the datagrid. In flex 2, a method nextPage() in PrintDataGrid will do that for you. printDataGridsave.mxml shows how to use that in flex 2.

Note, you should call updateLayout(); before you do addObject() to update the DataGrid layout to reflect the results of this function.

2. Can not print out header/footer area.

Print out header/footer area become much easier in flex 2.0. You can create a view, and you can include your own header/footer to specify the contents of the start and the end of the output. There is a sample in the doc you can use as a template.

If you just want to print out multiple components on a page, as in example printextra.mxml, you will need to put all the components into one container as you did in flex 1.5. However, you no longer need to set the print area explicitly.
See printextra2.mxml for example.

3. When the print area is larger than the screen, some of the content does not print out.

This is the same as it is in flex 1.5. You will need to set “clipContent=’false’ ” at the application tag (you may not have to set at the container level any more. You still need to set the application’s width/height to be large enough for the area you want to print.

4. When printing any chart, the print out always has gray background.

This is the same as it is in flex 1.5. You can either set the backgroundColor of the container that contains the chart to a color you desire, or explicitly set the fill of the chart.
Note, because the Properties are no longer bindable by default, you must declare them to be bindable by using the [Bindable] metadata tag before you define the dataProvider variable expenses: Array. See example chartprint.mxml.