Flex 4 打印预览

13 篇文章 0 订阅
12 篇文章 0 订阅
<?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);
			}
		}

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值