<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
    creationComplete="init(event)" 
    pageTitle="Printing A DataGrid That Spans Multiple Pages" viewSourceURL="srcview/index.html">

<mx:Script>
   <![CDATA[
            
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import flash.events.Event;
            import flash.net.URLLoader;
            import flash.net.URLRequest;
            import flash.utils.*;
            import mx.utils.ObjectUtil;
            import mx.collections.XMLListCollection;
            import mx.printing.FlexPrintJob;
            import mx.printing.FlexPrintJobScaleType;
            
          [Bindable]
          private var _submissionsXML:XMLListCollection;  


       /*
        Called automatically after the application
        is created
        */
        private function init(e:FlexEvent):void {
            
            //load the XML file that has the feed data
            var request:URLRequest = new URLRequest('data/submissions.xml');
            var loader:URLLoader = new URLLoader();
        
            //call function completeListener when the 
            //file is completely loaded
            loader.addEventListener(Event.COMPLETE, completeListener);
            
            //call function ioErrorListener if the 
            //file cannot be loaded
            loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
            
            loader.load(request);
           
                 
        }//end function init
            
            
        /*
        Function called automatically if the URLLoader object
        cannot successfully load the file
        
        */
        private function ioErrorListener(event:IOErrorEvent):void {
            
            
            Alert.show("Failure! Unable to load the data file.  Contact Bruce Phillips, webmaster@BrucePhillips.name to report this problem.\n" + event.text );
            
            
        }//end ioErrorListener
            
            
        /*
        Function called automatically when the file
        is completely loaded
        */
        private function completeListener(event:Event):void
        {
           //Alert.show( "Succes!" + '\n' + event.toString() )
           
           var loader:URLLoader = event.target as URLLoader;
            
            //convert the data in the file to XML object
             var _xml:XML = new XML(loader.data);
             
             var _xmlList:XMLList = _xml.row ;
             
             //Alert.show( _xml.toXMLString() );
             
              _submissionsXML = new XMLListCollection(_xmlList);
           
           //Alert.show( _submissionsXML.toXMLString()  );
           

        }//end completeListener
        
        
    /*
    Called automatically when the Print  button is clicked
    Prints the data displayed in the datagrid component
    */
   
    public function doPrint():void {
        
        // Create a FlexPrintJob instance.
        var printJob:FlexPrintJob = new FlexPrintJob();
        
        //returns true if user clicks on the print
        //dialog's OK button
        if ( printJob.start() ) {
            
            // Create a MyPrintView control 
            // this custom component contains
            //what needs to be printed
            var thePrintView:MyPrintView = new MyPrintView();
            
            //add the MyPrintView control to this application
            addChild(thePrintView);
            
            
            
            // Set the data provider of the MyPrintView 
            // component's DataGrid to be the data provider of 
            // the displayed DataGrid.
            thePrintView.printDG.dataProvider = submissionsDG.dataProvider;
            
             // Set the print view properties.
             thePrintView.width=printJob.pageWidth;
             thePrintView.height=printJob.pageHeight;
             
            // If the print DataGrid can hold all the  
            // data provider's rows on a single page, 
            //add the page to the print job. 
            if( thePrintView.printDG.validNextPage  == false )
            {
                printJob.addObject(thePrintView);
                
            }
            // else we have multiple pages to print
            else
            {
                
                //add the first page to the printJob
                printJob.addObject(thePrintView);
                
                //loop as long as there is another page to print
                
                while ( thePrintView.printDG.validNextPage ) {
                    
                    //get the next page
                    thePrintView.printDG.nextPage();
                    
                    //add this page to the printJob
                    printJob.addObject(thePrintView);
                    
                }//end while ( thePrintView.printDG.validNextPage )
                
             
                
            }//end if( thePrintView.printSessionDG.validNextPage == false )
            
            // All pages are queued; remove the MyPrintView 
            // control to free memory.
            removeChild(thePrintView);
            
            // Send the job to the printer
            //(each object added will be a separate printed page) 
            printJob.send();
            
        }//end  if (printJob.start()) 
         
        
    }//end function doPrint()
        
    ]]>
</mx:Script>

<mx:Label text="Click the button to print the results below."  fontSize="14"/>

<mx:Button label="Print" width="150" click="doPrint()"  fontSize="12"/>

<mx:DataGrid width="90%" height="90%" variableRowHeight="true" wordWrap="true"
     dataProvider="{_submissionsXML}" id="submissionsDG">
    
    <mx:columns>
        
        <mx:DataGridColumn dataField="MAINPRESENTER" headerText="Main Presenter" wordWrap="true" width="150" />
        <mx:DataGridColumn dataField="PRES_TITLE" headerText="Title" wordWrap="true" />
                
        
    </mx:columns>
    
    
</mx:DataGrid>

    
</mx:Application>