In the previous tutorial, we knew how to use flex build-in print class-Flexprintjob. It's simple to use. But you may face some challenges when print multiple pages. here is why:
To start to print on a new page, you need to write a code like this:
Printjob. addobject (targetcomponent );
It adds your componentFlexprintjob.But how many pages is it going to take?
The typical example is DataGrid. it might contains 1/1000 records. there is no way we can predetermine the size when binding with a dynamic data provider. plus how do you know where to put the page break, like record 1-17 on page 1, 18-30 on page 2 ...?
Fortunately, flex gives you a workaround to solve DataGrid printing issue. Here is how:
Things you need
- MX. Printing.Flexprintjob
- MX. Printing.Printdatagrid
Steps you do
1. Create a flexprintjob instance VaR flexprintjob: flexprintjob = new flexprintjob (); 2. Start flexprintjob Flexprintjob. Start (); 3. Create printdatagrid VaR myprintdata: printdatagrid = new printdatagrid (); application. application. addchild (myprintdata ); 4. Set printdatagrid's dataprovider (from DataGrid), width, and height Myprintdata. dataprovider = mydata. dataprovider; myprintdata. width = printjob. pagewidth; myprintdata. Height = printjob. pageheight; 5. Loop printdatagrid to generate multiple print pages Printjob. addobject (myprintdata); While (myprintdata. validnextpage) {myprintdata. nextpage (); printjob. addobject (myprintdata );} 6. Print Printjob. Send ();
Sample Code
<? XML version = "1.0" encoding = "UTF-8"?> <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" initialize = "Init ()"> <mx: SCRIPT> <! [CDATA [import MX. printing. printdatagrid; import MX. printing. flexprintjob; import MX. collections. arraycollection; [Bindable] public var datasource: arraycollection = new arraycollection (); Private var totalrecords: Number = 100; Private function Init (): void {for (var I: int = 1; I <= totalrecords; I ++) {var dataobject: Object = new object (); dataobject. name = "name #" + I; dataobject. phone = "Phone #" + I; dataobject. address = "address #" + I; datasource. additem (dataobject) ;}} private function doprint (): void {var printjob: flexprintjob = new flexprintjob (); If (printjob. start () {var myprintdata: printdatagrid = new printdatagrid (); application. application. addchild (myprintdata); myprintdata. dataprovider = mydata. dataprovider; myprintdata. width = printjob. pagewidth; myprintdata. height = printjob. pageheight; printjob. addobject (myprintdata); While (myprintdata. validnextpage) {myprintdata. nextpage (); printjob. addobject (myprintdata);} application. application. removechild (myprintdata); printjob. send () ;}}]> </MX: SCRIPT> <mx: panel Title = "flex tutorial-printdatagrid" width = "500" Height = "500" horizontalcenter = "0" verticalcenter = "0" horizontalalign = "center" verticalalign = "Middle"> <mx: dataGrid id = "mydata" dataprovider = "{datasource}" width = "400" Height = "400"/> <mx: button label = "print" Click = "doprint () "/> </MX: Panel> </MX: Application>
Conclusion
Adobe Flex provides printdatagrid to solve the problem of multiple pages printing. It works fine when your application mainly contains one big DataGrid.
However, life is not that simple. In our cases, you need to have a combination of different contains and controls likeCanvas, vbox, hbox, label, text, text area, image,PlusDataGrid. Unfortunately, until Flex 3, Adobe has not provided any better solution beyond flexprintjob and printdatagrid.
So, is there any 3rd party solution? I have done a lot of research. Eventually it narrows down to an open source project. This leads to our next tutorial-printing multiple pages using flexreport.
Source: http://flextutorial.org/2009/05/28/printing-multiple-pages-using-printdatagrid/