mx.printing
public class FlexPrintJob
继承FlexPrintJob Inheritance Object

FlexPrintJob 类是 flash.printing.PrintJob 类的包装对象。它支持自动对多个页面上的输出进行切片和分页,并且将网格内容进行缩放以适合打印机的页面大小。

查看示例

另请参见

About printing by using Flex classes
Using the FlexPrintJob class


公共属性
 属性定义方
 Inheritedconstructor : Object
对类对象或给定对象实例的构造函数的引用。
Object
  pageHeight : Number
[只读 (read-only)] 打印机页面上可打印区域的高度;不包括用户设置的任何边距。
FlexPrintJob
  pageWidth : Number
[只读 (read-only)] 打印机页面上可打印区域的宽度;不包括用户设置的任何边距。
FlexPrintJob
  printAsBitmap : Boolean
指定是以位图 (true) 还是以向量格式 (false) 打印作业内容。
FlexPrintJob
 Inheritedprototype : Object
[静态] 对类或函数对象的原型对象的引用。
Object
公共方法
 方法定义方
  
构造函数。
FlexPrintJob
  
addObject(obj:IUIComponent, scaleType:String = "matchWidth"):void
将 UIComponent 对象添加到正在打印的对象列表。
FlexPrintJob
 Inherited
指示对象是否已经定义了指定的属性。
Object
 Inherited
指示 Object 类的实例是否在指定为参数的对象的原型链中。
Object
 Inherited
指示指定的属性是否存在、是否可枚举。
Object
  
将添加的对象发送到打印机以开始打印。
FlexPrintJob
 Inherited
设置循环操作动态属性的可用性。
Object
  
初始化 PrintJob 对象。
FlexPrintJob
 Inherited
返回指定对象的字符串表示形式。
Object
 Inherited
返回指定对象的原始值。
Object
属性详细信息
pageHeight属性
pageHeight:Number  [只读 (read-only)]

打印机页面上可打印区域的高度;不包括用户设置的任何边距。在返回 start() 方法后设置它。



实现
    public function get pageHeight():Number
pageWidth属性 
pageWidth:Number  [只读 (read-only)]

打印机页面上可打印区域的宽度;不包括用户设置的任何边距。返回 start() 方法后设置此属性。



实现
    public function get pageWidth():Number
printAsBitmap属性 
printAsBitmap:Boolean  [读写]

指定是以位图 (true) 还是以向量格式 (false) 打印作业内容。以位图打印支持包含具有 alpha 透明度或颜色效果的位图图像的输出。如果内容不包含具有 alpha 透明度或颜色效果的任何位图图像,则您可以通过将 printAsBitmap 属性设置为 false 以较高质量向量格式打印。

默认值为 true.



实现
    public function get printAsBitmap():Boolean
    public function set printAsBitmap(value:Boolean):void
构造函数详细信息
FlexPrintJob()构造函数
public function FlexPrintJob()

构造函数。

方法详细信息
addObject()方法
public function addObject(obj:IUIComponent, scaleType:String = "matchWidth"):void

将 UIComponent 对象添加到正在打印的对象列表。返回 start() 方法后调用此方法。每次调用此方法都会启动新页面,因此您应该在页面大小的区块中编排对象格式。可以使用 PrintDataGrid 类来将数据网格扩展到多个页面中。

参数

obj:IUIComponent — 要打印的对象。
 
scaleType:String (default = "matchWidth") — 用于控制对象如何置于一个或多个打印页面上的缩放技术。必须是 FlexPrintJobScaleType 类中定义的常数值之一。

另请参见

send()方法 
public function send():void

将添加的对象发送到打印机以开始打印。使用 addObject() 方法添加打印页面后调用此方法。

start()方法 
public function start():Boolean

初始化 PrintJob 对象。向用户显示操作系统打印机对话框。返回此调用后,Flex 会设置 pageWidthpageHeight 属性。

返回
Boolean — 如果出现“打印”对话框时,用户单击“确定”,则返回 true;如果用户单击“取消”或出现错误,则返回 false
示例 如何使用示例
FormPrintHeader.mxml
<?xml version="1.0"?>
<!-- Custom control for the header area of the printed page. -->

<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="60%"
    horizontalAlign="right" >

    <mx:Label text="This is a placeholder for first page contents"/>
</mx:VBox>
FormPrintFooter.mxml
<?xml version="1.0"?>
<!-- Custom control for the footer area of the printed page. -->

<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="60%"
    horizontalAlign="right" >
    <!-- Declare and initialize the product total variable. -->

    <mx:Script>
        <![CDATA[
            [Bindable]
            public var pTotal:Number = 0;
        ]]>
    </mx:Script>
    <mx:Label text="Product Total: {pTotal}"/>
</mx:VBox>
FormPrintView.mxml
<?xml version="1.0"?>
<!-- Custom control to print the DataGrid control on multiple pages. -->

<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" backgroundColor="#FFFFFF"
    paddingTop="50" paddingBottom="50" paddingLeft="50">

    <mx:Script>
        <![CDATA[
            import mx.core.*
            // Declare and initialize the variables used in the component.
            // The application sets the actual prodTotal value.
            [Bindable]
            public var pageNumber:Number = 1;
            [Bindable]
            public var prodTotal:Number = 0;

            // Control the page contents by selectively hiding the header and
            // footer based on the page type.
            public function showPage(pageType:String):void {
                if(pageType == "first" || pageType == "middle") {
                    // Hide the footer.
                    footer.includeInLayout=false;
                    footer.visible = false;
                }
                if(pageType == "middle" || pageType == "last") {
                    // The header won't be used again; hide it.
                    header.includeInLayout=false;
                    header.visible = false;
                }
                if(pageType == "last") {
                    // Show the footer.
                    footer.includeInLayout=true;
                    footer.visible = true;
                }
                //Update the DataGrid layout to reflect the results.
                validateNow();
            }        
        ]]>
    </mx:Script>

    <!-- The template for the printed page, with the contents for all pages. -->
    <mx:VBox width="80%" horizontalAlign="left">
        <mx:Label text="Page {pageNumber}"/>
    </mx:VBox>

    <FormPrintHeader id="header" />
    <!-- The data grid. The sizeToPage property is true by default, so the last
        page has only as many grid rows as are needed for the data. -->
    <mx:PrintDataGrid id="myDataGrid" width="60%" height="100%">
    <!-- Specify the columns to ensure that their order is correct. -->
        <mx:columns>
            <mx:DataGridColumn dataField="Index" />
            <mx:DataGridColumn dataField="Qty" />
        </mx:columns>
    </mx:PrintDataGrid>

    <!-- Create a FormPrintFooter control and set its prodTotal variable. -->
    <FormPrintFooter id="footer" pTotal="{prodTotal}" />

</mx:VBox>
PrintDataGridExample.mxml
<?xml version="1.0"?>
<!-- Main application to print a DataGrid control on multiple pages. -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="initData();">

    <mx:Script>
        <![CDATA[

        import mx.printing.*;
        import mx.collections.ArrayCollection;
        import FormPrintView;

        // Declare variables and initialize simple variables.
        [Bindable]
        public var dgProvider:ArrayCollection;
        public var footerHeight:Number = 20;
        public var prodIndex:Number;
        public var prodTotal:Number = 0;

        // Data initialization.
        public function initData():void {
            // Create the data provider for the DataGrid control.
            dgProvider = new ArrayCollection;
        }

        // Fill the dgProvider ArrayCollection with the specified items.
        public function setdgProvider(items:int):void {

            prodIndex=1;
            dgProvider.removeAll();
            for (var z:int=0; z<items; z++)
            {
                var prod1:Object = {};
                prod1.Qty = prodIndex * 7;
                prod1.Index = prodIndex++;
                prodTotal += prod1.Qty;
                dgProvider.addItem(prod1);
            }
        }

        // The function to print the output.
        public function doPrint():void {

            var printJob:FlexPrintJob = new FlexPrintJob();
            if (printJob.start()) {
                // Create a FormPrintView control as a child of the current view.
                var thePrintView:FormPrintView = new FormPrintView();
               Application.application.addChild(thePrintView);

                //Set the print view properties.
                thePrintView.width=printJob.pageWidth;
                thePrintView.height=printJob.pageHeight;
                thePrintView.prodTotal = prodTotal;
                // Set the data provider of the FormPrintView component's data grid
                // to be the data provider of the displayed data grid.
                thePrintView.myDataGrid.dataProvider = myDataGrid.dataProvider;
                // Create a single-page image.
                thePrintView.showPage("single");
                // If the print image's data grid can hold all the provider's rows,
                // add the page to the print job.
                if(!thePrintView.myDataGrid.validNextPage)
                {
                    printJob.addObject(thePrintView);
                }
                // Otherwise, the job requires multiple pages.
                else
                {
                    // Create the first page and add it to the print job.
                    thePrintView.showPage("first");
                    printJob.addObject(thePrintView);
                    thePrintView.pageNumber++;
                    // Loop through the following code until all pages are queued.
                    while(true)
                    {
                        // Move the next page of data to the top of the print grid.
                        thePrintView.myDataGrid.nextPage();
                        thePrintView.showPage("last");
                        // If the page holds the remaining data, or if the last page
                        // was completely filled by the last grid data, queue it for printing.
                        // Test if there is data for another PrintDataGrid page.
                        if(!thePrintView.myDataGrid.validNextPage)
                        {
                            // This is the last page; queue it and exit the print loop.
                            printJob.addObject(thePrintView);
                            break;
                        }
                        else
                        // This is not the last page. Queue a middle page.
                        {
                            thePrintView.showPage("middle");
                            printJob.addObject(thePrintView);
                            thePrintView.pageNumber++;
                        }
                    }
                }
                // All pages are queued; remove the FormPrintView control to free memory.
                Application.application.removeChild(thePrintView);
            }
            // Send the job to the printer.
            printJob.send();
        }
        ]]>
    </mx:Script>

    <mx:Panel title="DataGrid Printing Example" height="75%" width="75%"
        paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">

        <mx:DataGrid id="myDataGrid" dataProvider="{dgProvider}">
            <mx:columns>
                <mx:DataGridColumn dataField="Index"/>
                <mx:DataGridColumn dataField="Qty"/>
            </mx:columns>
        </mx:DataGrid>

        <mx:Text width="100%" color="blue"
            text="Specify the number of lines and click Fill Grid first. Then you can click Print."/>

        <mx:TextInput id="dataItems" text="35"/>

        <mx:HBox>
            <mx:Button id="setDP" label="Fill Grid" click="setdgProvider(int(dataItems.text));"/>
            <mx:Button id="printDG" label="Print" click="doPrint();"/>
        </mx:HBox>
    </mx:Panel>
</mx:Application>