Printing from a DataGrid, Chapter 12, Solution 4

­Chapter_12_Flex_Sol_4_expert_More
<?xml version="1.0"?>
<!--
////////////////////////////////////////////////////////////////////////////////
// Flex Solutions: Essential Techniques for Flex 2 and Flex 3 Developers
// Author: Marco Casario 
// Editor: FriendsOfED www.friendsofed.com
// All Rights Reserved.
//
// Chapter 12: More Flex framework libraries and utilities
// 
// Solution 12-4: Printing from a DataGrid 
//
// 
// @author      Marco Casario
// @date        26 November 2007
// @version     1.0
// @site        flexsolutions.comtaste.com
//
////////////////////////////////////////////////////////////////////////////////
-->
 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[
        	import mx.printing.FlexPrintJobScaleType;
        	import mx.printing.PrintDataGrid;
        	import mx.printing.FlexPrintJob;
        	import mx.controls.dataGridClasses.DataGridColumn;
        	import mx.collections.ArrayCollection;
			import com.comtaste.print.ProductDatagridPrintView;
 
	        [Bindable]
	        private var productAC:ArrayCollection;
 
	        public function onCreationComplete():void
	        {
	            productAC = new ArrayCollection();
 
	            populateProducts(50);
	        }
 
	        private function populateProducts(length:int):void
	        {
	        	var obj:Object;
	        	var mytxt:String;
 
	        	for (var i:int; i < length; i++)
	        	{
	        		obj = new Object();
	        		obj.product = "product " + (i + 1).toString();
	        		obj.quantity = Math.ceil(Math.random() * length);
	        		obj.price = Math.random() * length;
 
	        		productAC.addItem(obj);
	        	}
	        }
 
	        public function myPrintJob():void {
	         // Create a FlexPrintJob instance.
            var printJob:FlexPrintJob = new FlexPrintJob();
            // Start the print job.
            if (printJob.start()) 
            {
                // Create a FormPrintView control as a child of the current view.
                var printComp:ProductDatagridPrintView = new ProductDatagridPrintView();
                addChild(printComp);
 
                //Set the print view properties.
                printComp.width=printJob.pageWidth;
                printComp.height=printJob.pageHeight;
                printComp.productDataGrid.dataProvider = dataGrid.dataProvider;
 
                if(!printComp.productDataGrid.validNextPage)
                {
                    printJob.addObject(printComp);
                }
                // Otherwise, the job requires multiple pages.
                else
                {
 
                    printJob.addObject(printComp);
 
                    // Loop through the following code until all pages are queued.
                    while(true)
                    {
                        // Move the next page of data to the top of the print grid.
                        printComp.productDataGrid.nextPage();
 
                        // Test if there is data for another PrintDataGrid page.
                        if(!printComp.productDataGrid.validNextPage) 
                        {
                            // This is the last page; queue it and exit the print loop.
                            printJob.addObject(printComp);
                            break;
                        }
                        else
                        // This is not the last page. Queue a middle page. 
                        {
 
                            printJob.addObject(printComp);
 
                        }
                    }
                }
                // All pages are queued; remove the FormPrintView control to 
                // free memory.
                removeChild(printComp);
            }
            // Send the job to the printer.
            printJob.send();
        }
 
        ]]>
    </mx:Script>
 
    <mx:Panel title="Product list" height="75%" width="75%">
        <mx:DataGrid id="dataGrid" dataProvider="{productAC}">
            <mx:columns>
                <mx:DataGridColumn headerText="Product name" dataField="product" />
                <mx:DataGridColumn headerText="Product quantity" dataField="quantity" />
                <mx:DataGridColumn headerText="Product price" dataField="price" />
            </mx:columns>
        </mx:DataGrid>
 
		<mx:Text id="countText" text="Total number of products: {productAC.length.toString()}." />
 
		<mx:ControlBar>
			<mx:Button id="printButton" label="Print" click="{myPrintJob()}" />
		</mx:ControlBar>
 
    </mx:Panel>
</mx:Application>
 
<strong>Chapter_12_Flex_Sol_4_More</strong>
<?xml version="1.0"?>
<!--
////////////////////////////////////////////////////////////////////////////////
// Flex Solutions: Essential Techniques for Flex 2 and Flex 3 Developers
// Author: Marco Casario 
// Editor: FriendsOfED www.friendsofed.com
// All Rights Reserved.
//
// Chapter 12: More Flex framework libraries and utilities
// 
// Solution 12-4: Printing from a DataGrid 
//
// 
// @author      Marco Casario
// @date        01 April 2008
// @version     1.1
// @site        flexsolutions.comtaste.com
//
////////////////////////////////////////////////////////////////////////////////
-->
 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete()">
 
    <mx:Script>
        <![CDATA[
        	import mx.printing.FlexPrintJobScaleType;
        	import mx.printing.PrintDataGrid;
        	import mx.printing.FlexPrintJob;
        	import mx.controls.dataGridClasses.DataGridColumn;
        	import mx.collections.ArrayCollection;
 
	        [Bindable]
	        private var productAC:ArrayCollection;
 
	        public function onCreationComplete():void
	        {
	            productAC = new ArrayCollection();
 
	            populateProducts(50);
	        }
 
	        private function populateProducts(length:int):void
	        {
	        	var obj:Object;
	        	var mytxt:String;
 
	        	for (var i:int; i < length; i++)
	        	{
	        		obj = new Object();
	        		obj.product = "product " + (i + 1).toString();
	        		obj.quantity = Math.ceil(Math.random() * length);
	        		obj.price = Math.random() * length;
 
	        		productAC.addItem(obj);
	        	}
	        }
 
	        /**
	         * Creates a PrintDataGrid instance and loops inside it to print each page.
	         */
	        public function doPrint():void
	        {
	            var printJob:FlexPrintJob = new FlexPrintJob();
	            if (printJob.start()) {
	            	// create a PrintDataGrid
	            	var printDataGrid:PrintDataGrid = new PrintDataGrid();
	            	printDataGrid.visible = false;
 					printDataGrid.width = printJob.pageWidth;
 					printDataGrid.height = printJob.pageHeight;
 					printDataGrid.dataProvider = dataGrid.dataProvider;
 					// add it to the current application:
	            	application.addChild(printDataGrid);
 
 					// column names in the PrintDataGrid match the properties of the
 					// object in the data proivider array;
 					// change column names to custom ones:
 					for (var i:int = 0; i < printDataGrid.columns.length; i++) {
 						var col:DataGridColumn = printDataGrid.columns[i];
 
 						switch (col.headerText) {
 							case "product":
 								col.headerText = "Product name";
 								break;
							case "quantity":
								col.headerText = "Product quantity";
								break;
							case "price":
								col.headerText = "Product price";
								break;
 						}
 					}
 					printDataGrid.validateNow();
 
 					// print the first page and call for the next pages, if any:
 					printJob.addObject(printDataGrid, FlexPrintJobScaleType.MATCH_WIDTH);
 					while (printDataGrid.validNextPage) {
 						printDataGrid.nextPage();
 						printJob.addObject(printDataGrid, FlexPrintJobScaleType.MATCH_WIDTH);
 					}
 
 					// remove PrintDataGrid from current application:
 					application.removeChild(printDataGrid);
 
 					// add a status text, at the end:
 					// note that the colored background is also printed; a custom
 					// component can be provided, to set the background color to #FFFFFF (white)
 					printJob.addObject(countText, FlexPrintJobScaleType.NONE);
	            }
	            // start printing:
	            printJob.send();
 	        }
        ]]>
    </mx:Script>
 
    <mx:Panel title="Product list" height="75%" width="75%">
        <mx:DataGrid id="dataGrid" dataProvider="{productAC}">
            <mx:columns>
                <mx:DataGridColumn headerText="Product name" dataField="product" />
                <mx:DataGridColumn headerText="Product quantity" dataField="quantity" />
                <mx:DataGridColumn headerText="Product price" dataField="price" />
            </mx:columns>
        </mx:DataGrid>
 
		<mx:Text id="countText" text="Total number of products: {productAC.length.toString()}." />
 
		<mx:ControlBar>
			<mx:Button id="printButton" label="Print" click="{doPrint()}" />
		</mx:ControlBar>
 
    </mx:Panel>
</mx:Application>