Datagrid的打印预览与打印

时间:2023-02-05 08:53:11

转载自:http://benben.javaeye.com/blog/278191

添加了一些注释。

 

Flex中的打印技术:http://hi.baidu.com/woaidelphi/blog/item/ced9e9d7d5cb37d5a044dfb7.html

 

http://livedocs.adobe.com/flex/3_cn/langref/mx/printing/FlexPrintJob.html

 

Application:

DataGridPrint.mxml

 

<?xml version="1.0" encoding="utf-8"?>
<!-- printing/DGPrintCustomComp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    height="450"
    width="550">
    <mx:states>
        <mx:State name="printView">
            <mx:RemoveChild target="{myForm}"/>
            <mx:AddChild position="lastChild">
                <mx:Panel width="388" height="303" layout="absolute">
                    <mx:Image id="img" x="10" y="10"/>
                </mx:Panel>
            </mx:AddChild>
            <mx:AddChild position="lastChild">
                <mx:Button label="Back" click="currentState=&quot;&quot;"/>
            </mx:AddChild>
        </mx:State>
    </mx:states>

    <mx:Script>
        <![CDATA[
            import mx.printing.FlexPrintJob;
            import mx.graphics.ImageSnapshot;
            import mx.core.UIComponent;
            import myComponent.MyPrintView;
           
            //打印预览
            private function print(u:UIComponent):void{
             //设置当前状态为printView
              currentState="printView";
              //通过图像快照ImageSnapshot获得位图数据
                var bmp:BitmapData = ImageSnapshot.captureBitmapData(u);
                //转换为位图
                var i:Bitmap = new Bitmap(bmp);     
                //设置图像的源为位图i         
                img.source = i;
                img.scaleContent = true;
            }
   
   //打印
            public function doPrint():void {
                // Create a FlexPrintJob instance.
                //创建一个FlexPrintJob实例
                var printJob:FlexPrintJob = new FlexPrintJob();
               
               // var job:PrintJob=new PrintJob();
   
                // Start the print job.
                //启动打印
                if(printJob.start()) {
                    // Create a MyPrintView control as a child
                    // of the current view.
                    //创建一个MyPrintView控制器,添加到当前视图中
                    var formPrintView:MyPrintView = new MyPrintView();
                    addChild(formPrintView);  
                    // Set the print control's data grid data provider to be
                    // the displayed data grid's data provider.
                    //设置控件formPrintView的数据源为当前DataGrid控件的数据源
                    formPrintView.myDataGrid.dataProvider = myDataGrid.dataProvider;  
                    // Add the SimplePrintview control to the print job.
                    // For comparison, try setting the
                    // second parameter to "none".
                    //添加打印控件
                    printJob.addObject(formPrintView);
   
                    // Send the job to the printer.
                    //发送数据到打印机
                    printJob.send();
   
                    // Remove the print-specific control to free memory.
                    //移除打印控件,释放内存
                    removeChild(formPrintView);
                }
            }
        ]]>
    </mx:Script>

    <!-- The form to display-->
    <mx:Form id="myForm">
        <mx:FormHeading label="Contact Information"/>
        <mx:FormItem label="Name: ">
            <mx:TextInput id="custName" width="200" text="Samuel Smith" fontWeight="bold" />
        </mx:FormItem>
        <mx:FormItem label="Phone: ">
            <mx:TextInput id="custPhone" width="200" text="617-555-1212" fontWeight="bold" />
        </mx:FormItem>
        <mx:FormItem label="Email: ">
            <mx:TextInput id="custEmail" width="200" text="sam@sam.com" fontWeight="bold" />
        </mx:FormItem>

        <mx:FormHeading id="formhead" label="Product Information"/>
        <mx:DataGrid id="myDataGrid" width="300">
            <mx:dataProvider>
                <mx:Object Product="Flash" Code="1000"/>
                <mx:Object Product="Flex" Code="2000"/>
                <mx:Object Product="ColdFusion" Code="3000"/>
                <mx:Object Product="JRun" Code="4000"/>
            </mx:dataProvider>
        </mx:DataGrid>
        <mx:Button label="PrintView" click="print(myDataGrid)"/>
        <mx:FormItem label="Label"></mx:FormItem>
        <mx:Button id="myButton" label="Print" click="doPrint();" />
    </mx:Form>
</mx:Application>

 

 

Component:
MyPrintView. mxml

 

<?xml version="1.0" encoding="utf-8"?>
<!-- printing/myComponents/MyPrintView.mxml -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#FFFFFF" height="250" width="450" paddingTop="50"
  paddingLeft="50" paddingRight="50">

    <!-- The controls to print, a PrintDataGrid control. -->
    <mx:PrintDataGrid id="myDataGrid" width="100%">
        <mx:columns>
            <mx:DataGridColumn dataField="Code"/>
            <mx:DataGridColumn dataField="Product"/>
        </mx:columns>
    </mx:PrintDataGrid>
</mx:VBox>