今天的专业实践检查时,被要求添加一个报表的打印功能,GOD!我哪知道 Flex里怎么实现打印啊,原来想的方案是用游览器自带的打印功能,以为万事OK了,没想到。。。。
因为我们的项目里用的是组件开发,因为Flex中不存在一个工程下有多个页面的说法,因为主页面背景改不了,也就意味着用游览器打印只能打印当前显示的全部内容,而不仅仅是一个表格内的内容。。。。
中午回来,花了二三个小时上网找了一下资料,结果,找到了单独实现打印和打印预览的代码,皇天不负有心人啊。。。。
PS:以下的核心代码来自于网上,我只是将它们两个功能整合在了一起,应该会对要实现打印功能的朋友有帮助(因为打印应该都要先预览一下)。
Application:
DataGridPrint.mxml
<?xml version="1.0"?>
<!-- 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="""/>
</mx:AddChild>
</mx:State>
</mx:states>
<mx:Script>
<![CDATA[
import mx.printing.FlexPrintJob;
import myComponent.MyPrintView;
import mx.graphics.ImageSnapshot;
import mx.core.UIComponent;
private function print(u:UIComponent):void{
currentState="printView";
var bmp:BitmapData = ImageSnapshot.captureBitmapData(u);
var i:Bitmap = new Bitmap(bmp);
img.source = i;
img.scaleContent = true;
}
public function doPrint():void {
// Create a FlexPrintJob instance.
var printJob:FlexPrintJob = new FlexPrintJob();
// Start the print job.
if(printJob.start()) {
// Create a MyPrintView control as a child
// of the current view.
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.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 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"?>
<!-- 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="Product"/>
<mx:DataGridColumn dataField="Code"/>
</mx:columns>
</mx:PrintDataGrid>
</mx:VBox>
运行结果:
分享到:
相关推荐
DataGrid的打印预览和打印DataGrid的打印预览和打印DataGrid的打印预览和打印DataGrid的打印预览和打印DataGrid的打印预览和打印
flex中的datagrid的分页定位以及高亮显示
DataGrid的打印预览和打印.rar
flex动态生成datagrid表头,外加linechart的简单操作,值得学习!
在FLEX4中开发带有过滤功能的DATAGRID组件
FLEX的datagrid合计、平均值,很好的例子代码拿来就可以用
flex中dataGrid导出数据到excel中,不存在乱码问题
flex组件之DataGrid高级用法实例源码
FLEX4实践—动态生成DataGrid及应用客户化itemRenderer.doc
vb.net编写的打印和打印预览datagrid中的数据 数据库为access的
Flex 动态给DataGrid增加行,可编辑行数据
Flex中导出当前DataGrid中的数据。生成一个Excel文件。资源为一个简单的应用。
Flex Builder4.5导入Excel和CSV文件到DataGrid中,并自定义DataGrid选中行的右键
flex 数据绑定dataGrid.rar
很好用的动态创建DataGrid并加入图片的方式。 只需要继承和实现一个接口就可以了。
重写DataGrid控件,用于打印DataGrid显示出的内容
flex做的datagrid留言板,通过webservice与数据库进行交互
简单的一个自定义label.根据dataGrid的数据变色.压缩包里一个是渲染器..一个是主程序....新建一个flex项目...把mxml和文件夹都放在scr下面就可以跑起来了..有问题请回复.