Flex提供了一個特殊的類FlexPrintJob來列印Flex對象。
FlexPrintJob可用於列印一個或多個Flex對象,如窗體或VBox容器。
FlexPrintJob列印對象及其包含的所有對象。
對象可以是顯示界面的全部或部分。
對象可以是專門爲列印格式化數據的組件。
FlexPrintJob類允許您縮放輸出以適合頁面。
FlexPrintJob類自動使用多個頁面列印不適合單個頁面的對象。
FlexPrintJob類使作業系統顯示列印對話框。如果沒有用戶操作,則無法列印。
Prepare and send a print job
您可以通過準備和發送列印作業來列印輸出。讓我們創建FlexPrintJob類的實例
var printJob:FlexPrintJob = new FlexPrintJob();
啓動列印作業
printJob.start();
Flex將使作業系統顯示一個列印對話框。將一個或多個對象添加到列印作業並指定如何縮放它們
printJob.addObject(myObject, FlexPrintJobScaleType.MATCH_WIDTH);
每個對象都從一個新頁面開始。將列印作業發送到印表機
printJob.send();
Printing Example
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.tutorialspoint.client as explained in the Flex - Create Application chapter. |
2 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to make sure business logic is working as per the requirements. |
<?xml version = "1.0" encoding = "utf-8"?> <s:Application 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 = "100%" height = "100%" minWidth = "500" minHeight = "500" initialize = "application_initializeHandler(event)"> <fx:Style source = "/com/tutorialspoint/client/Style.css" /> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; import mx.printing.FlexPrintJob; import mx.printing.FlexPrintJobScaleType; protected function btnClickMe_clickHandler(event:MouseEvent):void { // Create an instance of the FlexPrintJob class. var printJob:FlexPrintJob = new FlexPrintJob(); // 啓動列印作業. if (printJob.start() != true) return; // Add the object to print. Do not scale it. printJob.addObject(myDataGrid, FlexPrintJobScaleType.NONE); // Send the job to the printer. printJob.send(); } protected function application_initializeHandler(event:FlexEvent):void { lblHeader.text = "My Hello World Application"; } ]]> </fx:Script> <s:BorderContainer width = "500" height = "500" id = "mainContainer" styleName = "container"> <s:VGroup width = "100%" height = "100%" gap = "50" horizontalAlign = "center" verticalAlign = "middle"> <s:Label id = "lblHeader" fontSize = "40" color = "0x777777" styleName = "heading" /> <mx:DataGrid id = "myDataGrid" width = "300"> <mx:dataProvider> <fx:Object Product = "Flex" Code = "1000" /> <fx:Object Product = "GWT" Code = "2000" /> <fx:Object Product = "JAVA" Code = "3000" /> <fx:Object Product = "JUnit" Code = "4000" /> </mx:dataProvider> </mx:DataGrid> <s:Button label = "Print Me!" id = "btnClickMe" click = "btnClickMe_clickHandler(event)" styleName = "button" /> </s:VGroup> </s:BorderContainer> </s:Application>
完成所有更改後,讓我們以正常模式編譯並運行應用程式,就像我們在flex-create application一章中所做的那樣。如果您的應用程式一切正常,它將產生以下結果:[聯機試用]
單擊print me按鈕,您可以看到下面顯示的數據網格的列印輸出。