Flex 4 打印预览
程序员文章站
2022-05-05 16:14:38
...
<?xml version="1.0" encoding="utf-8"?> <s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:parsley="http://www.spicefactory.org/parsley" close="titlewindow1_closeHandler(event)" height="{FlexGlobals.topLevelApplication.height - 100}" width="100%" <fx:Declarations> </fx:Declarations> <fx:Script> <![CDATA[ import mx.containers.Canvas; import mx.controls.Alert; import mx.core.FlexGlobals; import mx.core.IUIComponent; import mx.core.IVisualElement; import mx.events.CloseEvent; import mx.events.FlexEvent; import mx.managers.PopUpManager; import mx.printing.*; import mx.utils.ObjectUtil; import spark.components.List; [Bindable] public var racingModel : RacingModel; [Bindable] public var racingSearchPM : RacingSearchPM; [Bindable] public var raceGridHeight : int; [Bindable] public var greyHoundGridHeight : int; [Bindable] public var harnessGridHeight : int; public static const DEFAULT_GRID_HEIGHT:int = 100; public static const CLOSE_POPUP:String = "closePopup"; private var _toBePrintData:IUIComponent; private var _printMulipleData:IUIComponent; private var printDataParent:DisplayObjectContainer; [Bindable]public var printScrollContent:Boolean=false; [Bindable]public var dateDisplayString:String; [Bindable]public var printTitle:String; [Bindable]public var jurisdictionID : int; private var sortedSizeWidth:Number; private var sortedSizeHeight:Number; /** * * Function to remove Popup * */ public function onClickClose(event : MouseEvent = null) : void { this.dispatchEvent(new Event(CLOSE_POPUP)); PopUpManager.removePopUp(this); } protected function imageLogo_ioErrorHandler(event:IOErrorEvent):void { // TODO Auto-generated method stub imageLogo.visible = false; } public function printInfo(event : MouseEvent = null):void { var printReceipt:PrintUtil = new PrintUtil(); printReceipt.printComponentToScaledWidth(printThisComponent); } /** * Set functioin of ' toBePrintData ' */ public function set toBePrintData(component:IUIComponent):void { if(component != null) { this._toBePrintData = component; if(!printScrollContent) { takeSnapshot(toBePrintData); } else { flattenVScrollbarAndTakeSnap(toBePrintData); } } else Alert.show("Print data is not available."); } private function flattenVScrollbarAndTakeSnap(component:IUIComponent):void { saveAndChangePrintObjectSize(component); component.addEventListener(FlexEvent.UPDATE_COMPLETE, takeSnapshopAfterUpdated); } private function takeSnapshopAfterUpdated(event:FlexEvent):void { var component:IUIComponent = event.target as IUIComponent; try { takeSnapshot(component); } catch(e:*) { Alert.show("Print data error!"); onClickClose(); } finally { component.removeEventListener(FlexEvent.UPDATE_COMPLETE, takeSnapshopAfterUpdated); restorePrintObjectSize(component); } } private function saveAndChangePrintObjectSize(component:IUIComponent):void { sortedSizeWidth = component.width; sortedSizeHeight = component.height; var contentWidth:Number; var contentHeight:Number; if(component is List) { contentWidth = component.measuredWidth; contentHeight = (component as List).scroller.viewport.contentHeight; }else{ contentWidth = component.measuredWidth; contentHeight = component.measuredHeight; } component.setActualSize(contentWidth, contentHeight); } private function restorePrintObjectSize(component:IUIComponent):void { component.setActualSize(sortedSizeWidth, sortedSizeHeight) } //take snapshot for a component. private function takeSnapshot(component:IUIComponent):void { var bmpData:BitmapData; var bmp:Bitmap; var uIcom:Canvas; bmpData = new BitmapData(component.width, component.height); bmpData.draw(component); bmp = new Bitmap(bmpData); uIcom = new Canvas(); uIcom.rawChildren.addChild(bmp); /* * TODO : rather simply assigning dimensions - check for compatibility with Paper Size, too */ uIcom.width = bmp.width; uIcom.height = bmp.height; printThisComponent.addElement(uIcom as IVisualElement); } /** * Get functioin of ' toBePrintData ' */ public function get toBePrintData():IUIComponent { return _toBePrintData; } public function toBePrintMulipleData(arrComponents:Array):void { var bmpData:BitmapData; var bmp:Bitmap; var uIcom:Canvas; if(arrComponents != null || arrComponents.length >= 1) { // to add multiple component var i : int; for ( i = 0; i < arrComponents.length; i++) { this._printMulipleData = arrComponents[i] as IUIComponent; bmpData = new BitmapData(_printMulipleData.width, _printMulipleData.height); bmpData.draw(_printMulipleData); bmp = new Bitmap(bmpData); uIcom = new Canvas(); uIcom.rawChildren.addChild(bmp); /* * TODO : rather simply assigning dimensions - check for compatibility with Paper Size, too */ uIcom.width = bmp.width; uIcom.height = bmp.height; printThisComponent.addElement(uIcom as IVisualElement); } } else Alert.show("Print data is not available."); } protected function titlewindow1_closeHandler(event:CloseEvent):void { // TODO Auto-generated method stub printThisComponent.removeAllElements(); this.removeEventListener(CloseEvent.CLOSE, titlewindow1_closeHandler); PopUpManager.removePopUp(this); } protected function onPrintHolderUpdateComplete(event:FlexEvent):void { } ]]> </fx:Script> <s:HGroup width="100%" top="10" horizontalAlign="right" paddingRight="20"> <s:Button label="Print" buttonMode="true" toolTip="Print"/> </s:HGroup> <s:Scroller width="100%" height="100%" top="35" verticalScrollPolicy="on" horizontalScrollPolicy="off"> <s:VGroup width="100%" horizontalAlign="center" paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10"> <s:VGroup id="printThisComponent" width="100%" height="100%" paddingBottom="20" top="90" updateComplete="onPrintHolderUpdateComplete(event)" verticalAlign="top"> <s:HGroup horizontalAlign="left" paddingLeft="30" verticalAlign="middle"> <mx:Image id="imageLogo" source="{resourceManager.getString(ConfigEnum.NEO_APP_CONFIGURATION_BUNDLE, 'accountmanagement.createaccount.nswjurisdiction.logo')}" height="34" visible="{jurisdictionID==ConfigEnum.JURISDICTION_NSW}" includeInLayout="{jurisdictionID==ConfigEnum.JURISDICTION_NSW}"/> <mx:Image id="imageLogovic" source="{resourceManager.getString(ConfigEnum.NEO_APP_CONFIGURATION_BUNDLE, 'accountmanagement.createaccount.vicjurisdiction.logo')}" height="34" visible="{jurisdictionID==ConfigEnum.JURISDICTION_VIC}" includeInLayout="{jurisdictionID==ConfigEnum.JURISDICTION_VIC}"/> <mx:Spacer width="100%" /> </s:HGroup> <s:VGroup width="100%" includeInLayout="{printScrollContent}" gap="0"> <s:SkinnableContainer width="100%" height="35" <s:Label text="{printTitle}" styleName="largeSectionHeading" left="10" verticalCenter="0"/> </s:SkinnableContainer> <s:Label text="{dateDisplayString}" height="50" verticalAlign="middle" width="100%" fontSize="14" fontWeight="bold" color="#000000"/> </s:VGroup> </s:VGroup> </s:VGroup> </s:Scroller> </s:TitleWindow>
打印按钮触发的事件函数
public function print(obj:UIComponent, printForScorllContent:Boolean=false):void { if (printPreviewPopUp) { PopUpManager.removePopUp(printPreviewPopUp); } printPreviewPopUp = new PrintPreviewPopUp(); printPreviewPopUp.printScrollContent = printForScorllContent; if(printForScorllContent) { printPreviewPopUp.printTitle = headerLabel; printPreviewPopUp.dateDisplayString = dateDisplayString; } printPreviewPopUp.jurisdictionID = applicationModel.getJurisdictionId(); PopUpManager.addPopUp(printPreviewPopUp as IFlexDisplayObject, FlexGlobals.topLevelApplication as DisplayObject, true); if(obj.height>0 && obj.width>0) { isPrinting = true; printPreviewPopUp.toBePrintData = obj; printPreviewPopUp.x = FlexGlobals.topLevelApplication.width/2 - obj.width/2; printPreviewPopUp.y = 50; PopUpManager.bringToFront(printPreviewPopUp as IFlexDisplayObject ); printPreviewPopUp.addEventListener(CloseEvent.CLOSE, onPrintPreviewPopupClosed); } }
推荐阅读
-
将PowerPoint中的每两页内容打印到一张A4纸上以节省纸张
-
Word2010打印预览时前面多出了两个空白页表现为无法选中状态
-
Win10 RS4快速预览版17093开始推送(附更新、修复内容)
-
剑指offer59:按之字形顺序打印二叉树:[[1], [3,2], [4,5,6,7]]
-
js实现局部页面打印预览原理及示例代码_javascript技巧
-
Java中打印日志,这4点很重要!
-
打印,打印设置,打印预览 打印打印设置打印预览webbrowserjavascript
-
Log4j 配置日志打印时区的实现方法
-
Log4j 配置日志打印时区的实现方法
-
详解SSM框架下结合log4j、slf4j打印日志