Flex 自定义打印控件编写
打印历来是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> |
Flex 自定义打印控件编写的更多相关文章
- Android实现图片轮显效果——自定义ViewPager控件
一.问题概述 使用ViewPager控件实现可横向翻页.水平切换图片等效果,但ViewPager需要手动滑动才能切换页面,图片轮显效果的效果本质上就是在ViewPager控件的基础上让它能自动的进行切 ...
- 网页WEB打印控件制作-开放源码
在WEB系统中,打印的确是比较烦人的问题,如果我们能制作一个属于自己的自定义的打印插件,那么我们在后续自定义打印的时候能随心所欲的控制打印,这样的效果对于程序员来说是非常开心的一件事件,本文将自己开发 ...
- 网页WEB打印控件
网页WEB打印控件制作 在WEB系统中,打印的确是比较烦人的问题,如果我们能制作一个属于自己的自定义的打印插件,那么我们在后续自定义打印的时候能随心所欲的控制打印,这样的效果对于程序员来说是非常开心的 ...
- C# DataGridView自定义分页控件
好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...
- Android 手机卫士--自定义组合控件构件布局结构
由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...
- [转].net自定义验证控件CustomValidator的使用
本文转自:http://tech.cncms.com/web/aspnet/96310.html CustomValidator验证控件,可以自定义验证函数,实现其它几个验证控件不能实现的验证规则,最 ...
- (九)ASP.NET自定义用户控件(2)
http://www.cnblogs.com/SkySoot/archive/2012/09/04/2670678.html 用户控件 在 .NET 里,可以通过两种方式把自己的控件插入到 Web 窗 ...
- WEB免费打印控件推荐
在WEB系统中,打印的确是个烦人的问题. 要么自己开发打印控件,如果项目时间紧,肯定来不及. 要么购买成熟的打印控件,如果是大项目可以考虑,但如果项目只有几K到1.2W之间,这就麻烦了. 前段时间有机 ...
- Flex 教程(1)-------------控件拖动
今天和大家分享下关于在Flex中 针对控件的拖动开发. 1.需要在 .mxml文件中编写一个Button按钮 如下代码: <s:Button id="button1" lab ...
随机推荐
- sqlalchemy多表查询
from datetime import datetime from sqlalchemy import Column,Integer,String,Boolean,DateTime,ForeignK ...
- UVA 10229 Modular Fibonacci
斐波那契取MOD.利用矩阵快速幂取模 http://www.cnblogs.com/Commence/p/3976132.html 代码: #include <map> #include ...
- JAVA快速功能
1.日期格式化 Date date=new Date(); //转换成时间格式12小时制 SimpleDateFormat df_12=new SimpleDateFormat("yyyy- ...
- postman的使用(转载)
Postman介绍 Postman是google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件.其主要功能包括: 模拟各种HTTP requests 从常用的 ...
- 解决ASP.NET裁剪图片失真
//有的时候剪切图片时,出现图片失真的问题,是因为图片质量下降造成的 //按照指定的数据画出画板(位图) System.Drawing.Image imgPhoto = System.Drawing. ...
- ORA-01940: cannot drop a user that is currently connected
https://www.cnblogs.com/lwlxqlccc/p/8694696.html
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- Web开发之编码与解码、签名、加密与解密
在Web开发中,编码与解码.签名.加密与解密是非常常见的问题.本文不会介绍具体实例,而是介绍这些的原理.用途与区别.一.编码与解码 在Web开发中,需要通过URL的query参数来传递数 ...
- Python的并发并行[2] -> 队列[1] -> 使用队列进行任务控制
使用队列进行任务控制 1 FIFO与LIFO队列 FIFO(First In First Out)与LIFO(Last In First Out)分别是两种队列形式,在FIFO中,满足先入先出的队列方 ...
- [Python Cookbook] Pandas Groupby
Groupby Count # Party’s Frequency of donations nyc.groupby(’Party’)[’contb receipt amt’].count() The ...