topical media & game development

talk show tell print

professional-flex-code-08-PrintDataGridPrintJob.mx

professional-flex-code-08-PrintDataGridPrintJob.mx [swf] flex


  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="initData()">
    <mx:Script>
      <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.printing.FlexPrintJob;
      [Bindable]
      public var dp:ArrayCollection;
      // populate the dataProviders for the datagrids
      private function initData():void{
        this.dp = new ArrayCollection();
        for(var i:Number=0; i<60; i++){
          this.dp.addItem({row:i,data:"item " + i});
        }
      }
      public function print():void {
        // create print job
        var printJob:FlexPrintJob = new FlexPrintJob();
        // start print job
        if(printJob.start()){
          // add the print grid
          printJob.addObject(printGrid);
          while(true) {
            // loop through print grids data and add additonal pages if neccessary
            printGrid.nextPage();
            if(!printGrid.validNextPage){
              printJob.addObject(printGrid);
              break;
            }
          }
          // send to printer
          printJob.send();
        }
      }
      ]]>
    </mx:Script>
    <mx:DataGrid id="dataGrid" dataProvider="{dp}" height="200" width="200">
      <mx:columns>
        <mx:DataGridColumn dataField="row" headerText="Row Number" />
        <mx:DataGridColumn dataField="data" headerText="Row Data" />
      </mx:columns>
    </mx:DataGrid>
    <mx:Button click="print()" label="Print" />
    <mx:PrintDataGrid visible="false" id="printGrid" dataProvider="{dp}" height="700" width="600">
      <mx:columns>
        <mx:DataGridColumn dataField="row" headerText="Row Number" />
        <mx:DataGridColumn dataField="data" headerText="Row Data" />
      </mx:columns>
    </mx:PrintDataGrid>
  </mx:Application>
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.