Reproduced from: http://benben.javaeye.com/blog/278191
Added a few comments.
The printing technology in 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= ">"
<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= "ten" y= "ten"/>
</mx:Panel>
</mx:AddChild>
<mx:addchild position= "LastChild" >
<mx:button label= "Back" click= "currentstate=""" />
</mx:AddChild>
</mx:State>
</mx:states>
<mx:Script>
<! [cdata[
Import Mx.printing.FlexPrintJob;
Import Mx.graphics.ImageSnapshot;
Import mx.core.UIComponent;
Import Mycomponent.myprintview;
Print Preview
Private function Print (u:uicomponent): void{
Set current status to Printview
Currentstate= "Printview";
Get bitmap data through image snapshot Imagesnapshot
var bmp:bitmapdata = imagesnapshot.capturebitmapdata (u);
Convert to Bitmap
var i:bitmap = new Bitmap (BMP);
Set the source of the image to bitmap I
Img.source = i;
Img.scalecontent = true;
}
Print
Public Function Doprint (): void {
Create a Flexprintjob instance.
Create a Flexprintjob instance
var printjob:flexprintjob = new Flexprintjob ();
var job:printjob=new PrintJob ();
Start the print job.
Start printing
if (Printjob.start ()) {
Create a Myprintview control as a child
The current view.
Create a Myprintview controller to add to the current view
var formprintview:myprintview = new Myprintview ();
AddChild (Formprintview);
Set the print control ' s data grid data provider to IS
The displayed data grid ' s data provider.
To set the data source for a control Formprintview as the data source for the current DataGrid control
FormPrintView.myDataGrid.dataProvider = Mydatagrid.dataprovider;
ADD the Simpleprintview control to the print job.
For comparison, try setting the
Second parameter to "none".
Add a Print control
Printjob.addobject (Formprintview);
Send the job to the printer.
Send data to Printer
Printjob.send ();
Remove the Print-specific control to free memory.
To remove a print control and free up 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= text= "Samuel Smith" fontweight= "bold"/>
</mx:FormItem>
<mx:formitem label= "Phone:" >
<mx:textinput id= "Custphone" width= "text=" "617-555-1212" fontweight= "bold"/>
</mx:FormItem>
<mx:formitem label= "Email:" >
<mx:textinput id= "Custemail" width= "text=" "sam@sam.com" fontweight= "bold"/>
</mx:FormItem>
<mx:formheading id= "Formhead" label= "Product Information"/>
<mx:datagrid id= "Mydatagrid" width= ">"
<mx:dataProvider>
<mx:object product= "Flash" code= "1000"/>
<mx:object product= "Flex" code= "/>"
<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= "width=" 450 = "50"
paddingleft= "paddingright=" >
<!--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>