打印历来是web应用一个比较棘手的问题,幸好flex web应用是运行在flash player上的,flash player可以访问打印机,所以flex 应用可以实现比较强大的打印功能。Flex 自身提供的printjob、flexprintjob相对来说用起来不是很方便,对于有严格纸张设置、翻页等打印需求,略显功能不足,因此需要基于printjob或flexprintjob来完善flex的打印功能,及自定义打印控件。
本控件核心为:
1)PrintManager,打印控制核心类
2)IPrintableDocument,打印文档设置,打印文档有3部分组成,分别是:PrintableHeader、PrintableBody、PrintableFooter。
因此,开发人员只要设计好printabledocument,重点排版好header、body、footer,然后调用PrintManager的打印即可打印printabledocument了。
<print:PrintableDocument printableBody="{p_body}" printableFooter="{p_footer}" printableHeader="{p_header}"> <print:PrintableHeader width="100%" id="p_header" x="0" y="0" height="81" > <mx:Label id="pageHeader" text="Sample Accounts" styleName="printTitle" width="100%" textAlign="center"/> <mx:Label id="cityHeader" text="City Of Somewheretown" width="100%" textAlign="center" /> <mx:Label id="entityHeader" text="General Accounts" width="100%" textAlign="center"/> </print:PrintableHeader> <print:PrintableBody id="p_body" width="100%" bottom = "28" x="0" top="81"> <mx:PrintAdvancedDataGrid id="accountsDataGrid" designViewDataType="flat" dataProvider="{accounts}" horizontalGridLines="false" verticalGridLines="false" width="100%" height="100%" borderStyle="none" sortExpertMode="true" headerHeight="0" rowHeight="30"> <mx:columns> <mx:AdvancedDataGridColumn headerText="Account Number" dataField="account" textAlign="center" width="100"/> <mx:AdvancedDataGridColumn headerText="Control Account" dataField="control" textAlign="center" width="100"/> <mx:AdvancedDataGridColumn headerText="Description" dataField="description" width="300"/> </mx:columns> </mx:PrintAdvancedDataGrid> </print:PrintableBody> <print:PrintableFooter id="p_footer" width="100%" height="28" bottom="0" left="0" printType="perpage"> <mx:Label text="Prepared by: Authorname" width="33%" textAlign="center"/> <mx:Label text="Page {pageNumber} of {pageCount}" width="34%" textAlign="right"/> <mx:Label id="pageFooter" text="Sample Accounts" width="33%"/> </print:PrintableFooter> </print:PrintableDocument> |
附上控件核心类的相关代码。
1、PrintManager
public class PrintManager { public function PrintManager() { } public function print(document:IPrintableDocument, parent:UIComponent = null):Boolean { var printJob:PrintJob = new PrintJob(); if (parent != null) parent.addChild(document.displayObject()); var printed:Boolean = printInternal(document, printJob); if (parent != null) parent.removeChild(document.displayObject()); return printed; } private functionunscaleDocument(document:IPrintableDocument):void { document.scaleX = 1.0; document.scaleY = 1.0; document.validateNow(); } /** * adjust print orientation * scale the printdocument */ private functionrotateDocumentForPaperOrientation(document:IPrintableDocument, printJob:PrintJob):void { document.x = 0; document.y = 0; var scaleFactor:Number = 1.0; // document.scaleX = scaleFactor; document.scaleY = scaleFactor; document.validateNow(); } private function printPage(document:IPrintableDocument, printJob:PrintJob):void { var width:Number = document.desiredWidth; var height:Number = document.desiredHeight; var page:Sprite = Sprite(document); var printableArea:Rectangle = new Rectangle(0, 0, width, height); printJob.addPage(page, printableArea); } public function printInternal(document:IPrintableDocument, printJob:PrintJob):Boolean { //start printer if (!printJob.start()) { return false; } document.moveToFirstPage(); unscaleDocument(document); //adjust paper orientation rotateDocumentForPaperOrientation(document, printJob); //add first print page printPage(document, printJob); while(document.validNextPage) { document.nextPage(); printPage(document, printJob); } printJob.send(); return true; } } |
2、IPrintableDocument
public interface IPrintableDocument extends IUIComponent, IEventDispatcher { function nextPage():void; function get validNextPage():Boolean; function previousPage():void; function moveToFirstPage():void; function validateNow():void; function displayObject():DisplayObject; function get desiredWidth():Number; function get desiredHeight():Number; function get printableFooter():PrintableFooter; function get printableHeader():PrintableHeader; function get printableBody():PrintableBody; function set printableBody(body:PrintableBody):void; function set printableHeader(header:PrintableHeader):void; function set printableFooter(footer:PrintableFooter):void; function get totalPageCount():Number; function get currentPageNumber():Number; } |
3、PrintableDocument
public class PrintableDocument extends VGroup implementsIPrintableDocument { [Bindable]public var pageNumber:Number = 1; [Bindable]public var pageCount:Number = 1; private var _body:PrintableBody; private var _header:PrintableHeader; private var _footer:PrintableFooter; private var _desiredWidth:Number = 595; private var _desiredHeight:Number = 842; // public function PrintableDocument(){ this.gap = 0; } public function get desiredWidth():Number { return _desiredWidth; //21cm, A4,1cm=28.346px } public function set desiredWidth(value:Number):void{ _desiredWidth = value; } public function set desiredHeight(value:Number):void{ _desiredHeight = value; } public function get desiredHeight():Number { return _desiredHeight; //29.7cm, A4 } public function get totalPageCount():Number{ return pageCount; } public function get currentPageNumber():Number{ return pageNumber; } public function displayObject():DisplayObject { return this; } public function get validNextPage():Boolean { return printableBody.printGrid.validNextPage; } public function nextPage():void { printableBody.printGrid.nextPage(); pageNumber++; if(pageNumber == 2) { removeHeaderFromLayout(); } if(!validNextPage){ includeFooterInLayout(); } validateNow(); } /** * move to previous page. */ public function previousPage():void { printableBody.printGrid.previousPage(); pageNumber--; if (pageNumber == 1) { includeHeaderInLayout(); } removeFooterFromLayout(); validateNow(); // dispatchEvent(new Event("validPagesChanged")); } /** * move to first page. */ public function moveToFirstPage():void { pageNumber = 1; printableBody.printGrid.moveToFirstPage(); includeHeaderInLayout(); removeFooterFromLayout(); validateNow(); } /** * calcutate page count. */ public function calculatePageCount():void { var count:Number=1; moveToFirstPage(); while(printableBody.printGrid.validNextPage) { count++; if(count == 2) { removeHeaderFromLayout(); } printableBody.printGrid.nextPage(); validateNow(); } pageCount = count; moveToFirstPage(); } private function includeHeaderInLayout():void { printableHeader.includeInLayout = true; printableHeader.visible = true; } private function removeHeaderFromLayout():void { if(printableHeader.printType==PrintableHeader.PRINTTYPE_FIRSTPAGE){ printableHeader.includeInLayout = false; printableHeader.visible = false; } } private function includeFooterInLayout():void{ printableFooter.includeInLayout = true; printableFooter.visible = true; } private function removeFooterFromLayout():void{ if(printableFooter.printType==PrintableFooter.PRINTTYPE_LASTPAGE && validNextPage){ printableFooter.includeInLayout = false; printableFooter.visible = false; } } public function get printableBody():PrintableBody{ return _body; } public function set printableBody(body:PrintableBody):void{ this._body = body; } public function get printableFooter():PrintableFooter{ return _footer; } public function setprintableFooter(footer:PrintableFooter):void{ this._footer = footer; } public function get printableHeader():PrintableHeader{ return this._header; } public function setprintableHeader(header:PrintableHeader):void{ this._header = header; } } |
4、PrintableHeader、PrintableBody、PrintableFooter代码
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Rect width="100%" height="100%"> <s:fill><s:SolidColor color="0xFF4500" /></s:fill> </s:Rect> <fx:Script> <![CDATA[ /** *[firstpage,perpage]<br/> *firstpage,header区域只在第一页打印<br/> *perpage,header区域每页都会打印 */ public var printType:String = "firstpage"; public static const PRINTTYPE_FIRSTPAGE:String ="firstpage"; public static const PRINTTYPE_PERPAGE:String = "perpage"; ]]> </fx:Script> </s:Group> Body: <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="500"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.printing.PrintAdvancedDataGrid; public var printGrid:PrintAdvancedDataGrid; ]]> </fx:Script> </s:Group> Footer: <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Rect width="100%" height="100%"> <s:fill><s:SolidColor color="0xFF00ff" /></s:fill> </s:Rect> <fx:Script> <![CDATA[ /** *[lastpage,perpage]<br/> *lastpage,footer区域只在最后一页打印<br/> *perpage,footer区域每页都会打印 */ public var printType:String = "lastpage"; public static const PRINTTYPE_LASTPAGE:String = "lastpage"; public static const PRINTTYPE_PERPAGE:String = "perpage"; ]]> </fx:Script> </s:Group> |