Flex提供了一个特殊的类 FlexPrintJob 来打印flex对象。
FlexPrintJob可用于打印一个或多个Flex对象,例如Form或VBox容器。
FlexPrintJob打印对象及其包含的所有对象。
对象可以是显示的界面的全部或部分。
对象可以是格式化用于打印的数据的组件。
FlexPrintJob类允许您缩放输出以适应页面。
FlexPrintJob类自动使用多个页面来打印不适合单个页面的对象。
FlexPrintJob类使操作系统显示“打印"对话框。 如果没有某些用户操作,则无法打印。
准备并发送打印作业
通过准备和发送打印作业来打印输出。 让我们创建一个FlexPrintJob类的实例
- var printJob:FlexPrintJob = new FlexPrintJob();
开始打印作业
- printJob.start();
Flex将使操作系统显示“打印"对话框。 将一个或多个对象添加到打印作业,并指定如何缩放它们
- printJob.addObject(myObject, FlexPrintJobScaleType.MATCH_WIDTH);
每个对象从一个新页面开始。 将打印作业发送到打印机
- printJob.send();
打印示例
步骤 | 描述 |
---|---|
1 | 在 Flex - 创建应用程序章节中所述,在包 com.tutorialspoint.client 下创建名为 HelloWorld 的项目。 |
2 | 修改 HelloWorld.mxml ,如下所述。 保持文件的其余部分不变。 |
3 | 编译并运行应用程序,以确保业务逻辑按照要求工作。 |
以下是修改后的mxml文件 src / com.tutorialspoint / HelloWorld.mxml 的内容。
- <?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();
- // Start the print job.
- 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 - 创建应用程序中一样 章节。 如果一切顺利,您的应用程序,这将产生以下结果:[在线试用]

点击打印我按钮,您可以看到数据网格的打印输出如下所示。

转载本站内容时,请务必注明来自W3xue,违者必究。