Flex之打印篇
时间:2010-09-19 来源:风中绝响
使用两个类解决打印问题:FlexPrintJob和PringDataGrid。
? FlexPrintJob。这个类需要在实例化之后用作要打印对象的容器。
? PringDataGrid。是DataGrid的一个子类,用来打印必须以网格或表格形式显示的数据,支持多页面打印。
简单的打印实例:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=http://www.adobe.com/2006/mxml layout="absolute">
<mx:Script>
<![CDATA[
import mx.printing.FlexPrintJob;
private function testPrint():void{
var myPrintJob:FlexPrintJob = new FlexPrintJob();
if (myPrintJob.start() != true){
return;
}
//加入需要打印的组件
myPrintJob.addObject(printContent);
myPrintJob.send();
}
]]>
</mx:Script>
<mx:VBox id="printContent" backgroundColor="#FFFFFF">
<mx:Label text="打印示例">
</mx:Label>
</mx:VBox>
<mx:Button x="30" y="30" label="打印" click="testPrint()"/>
</mx:Application>
见源代码:printDemo.mxml。
创建单独的Print容器:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=http://www.adobe.com/2006/mxml layout="absolute" creationComplete="bookDataCall.send()">
<mx:Script>
<![CDATA[
import mx.printing.FlexPrintJob;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
[Bindable]
public var bookData:ArrayCollection;
//得到 books.xml数据
private function bookFunction(event:ResultEvent):void{
bookData = event.result.books.stock;
}
//打印方法
private function printJob():void{
var myPrintJob:FlexPrintJob = new FlexPrintJob();
if (myPrintJob.start() != true){
return ;
}
//绑定数据源
myPrintDataGrid.dataProvider = bookInfo.dataProvider;
//加入需要打印的组件
myPrintJob.addObject(printArea);
myPrintJob.send();
}
]]>
</mx:Script>
<mx:HTTPService id="bookDataCall" url="assets/books.xml" result="bookFunction(event)" />
<mx:Form id="myForm">
<mx:DataGrid id="bookInfo" dataProvider="{bookData}">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="书 名" />
<mx:DataGridColumn dataField="author" headerText="作 者" />
<mx:DataGridColumn dataField="category" headerText="目 录" />
</mx:columns>
</mx:DataGrid>
<mx:Button id="myButton" label="打印" />
</mx:Form>
<mx:VBox id="printArea" height="300" width="500" backgroundColor="#FFFFFF" visible="false">
<mx:PrintDataGrid id="myPrintDataGrid" height="100%" width="100%" fontSize="6" fontFamily="Arial" wordWrap="true"/>
</mx:VBox>
</mx:Application>
见源代码:printDemo1.mxml,books.xml
使用组件打印:
1. 组件代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="500" height="300" visible="false" backgroundColor="#FFFFFF">
<mx:PrintDataGrid id="myPringDataGrid" height="100%" width="100%" fontSize="6" fontFamily="Arial" wordWrap="true">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="书 名" />
<mx:DataGridColumn dataField="author" headerText="作 者" />
<mx:DataGridColumn dataField="category" headerText="目 录" />
</mx:columns>
</mx:PrintDataGrid>
</mx:VBox>
2. 应用代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=http://www.adobe.com/2006/mxml layout="absolute" creationComplete="bookDataCall.send()">
<mx:Script>
<![CDATA[
import mx.printing.FlexPrintJob;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import components.PrintComp;
[Bindable]
public var bookData:ArrayCollection;
//得到 books.xml数据
private function bookFunction(event:ResultEvent):void{
bookData = event.result.books.stock;
}
//打印方法
private function printJob():void{
var myPrintJob:FlexPrintJob = new FlexPrintJob();
if (myPrintJob.start() != true){
return ;
}
//实例化组件
var myPrintComp:PrintComp = new PrintComp();
//动态增加组件
this.addChild(myPrintComp);
//绑定数据源
myPrintComp.myPringDataGrid.dataProvider =
bookInfo.data;
//加入需要打印的组件
myPrintJob.addObject(myPrintComp);
myPrintJob.send();
//移除组件
this.removeChild(myPrintComp);
}
]]>
</mx:Script>
<mx:HTTPService id="bookDataCall" url="assets/books.xml"
result="bookFunction(event)" />
<mx:Form id="myForm">
<mx:DataGrid id="bookInfo" dataProvider="{bookData}">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="书 名" />
<mx:DataGridColumn dataField="author" headerText="作 者" />
<mx:DataGridColumn dataField="category" headerText="目 录" />
</mx:columns>
</mx:DataGrid>
<mx:Button id="myButton" label="打印" />
</mx:Form>
<mx:VBox id="printArea" height="300" width="500"
backgroundColor="#FFFFFF" visible="false">
<mx:PrintDataGrid id="myPrintDataGrid" height="100%"
width="100%" fontSize="6" fontFamily="Arial"
wordWrap="true"/>
</mx:VBox>
</mx:Application>
见源代码:printDemo2.mxml,books.xml