Apache Flex 4.16.1 API Reference | All Packages | All Classes | Index | Frames No Frames |
FlexPrintJob

Package | mx.printing
Class | public class FlexPrintJob
Inheritance | FlexPrintJob Object

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The FlexPrintJob class is a wrapper for the flash.printing.PrintJob class. It supports automatically slicing and paginating the output on multilple pages, and scaling the grid contents to fit the printer's page size.

View the examples

Public Properties

| Property | Defined By
--- | --- | ---
|  | pageHeight : Number [read-only] The height of the printable area on the printer page; it does not include any user-set margins. | FlexPrintJob
|  | pageWidth : Number [read-only] The width of the printable area on the printer page; it does not include any user-set margins. | FlexPrintJob
|  | printAsBitmap : Boolean Specifies whether to print the job content as a bitmap (true) or in vector format (false). | FlexPrintJob
|  | printJob : PrintJob [read-only] The printJob property; | FlexPrintJob

Public Methods

| Method | Defined By
--- | --- | ---
|  | FlexPrintJob() Constructor. | FlexPrintJob
|  | addObject(obj:IUIComponent, scaleType:String = matchWidth):void Adds a UIComponent object to the list of objects being printed. | FlexPrintJob
|  | send():void Sends the added objects to the printer to start printing. | FlexPrintJob
|  | start():Boolean Initializes the PrintJob object. | FlexPrintJob

Property Detail

pageHeight | property

`pageHeight:Number` [read-only]

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The height of the printable area on the printer page; it does not include any user-set margins. It is set after start() method returns.

Implementation
`public function get pageHeight():Number`

pageWidth | property |

`pageWidth:Number` [read-only]

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The width of the printable area on the printer page; it does not include any user-set margins. This property is set after `start()` method returns.

Implementation
`public function get pageWidth():Number`

printAsBitmap | property |

`printAsBitmap:Boolean`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Specifies whether to print the job content as a bitmap (`true`) or in vector format (`false`). Printing as a bitmap supports output that includes a bitmap image with alpha transparency or color effects. If the content does not include any bitmap images with alpha transparency or color effects, you can print in higher quality vector format by setting the `printAsBitmap` property to `false`.

The default value is `true`.

Implementation
`public function get printAsBitmap():Boolean`
`public function set printAsBitmap(value:Boolean):void`

printJob | property |

`printJob:PrintJob` [read-only]

Language Version : | ActionScript 3.0

Product Version : | Flex 4.9

Runtime Versions : | Flash Player 9, AIR 1.1

The printJob property;

Implementation
`public function get printJob():PrintJob`
Constructor Detail

FlexPrintJob | () | Constructor

`public function FlexPrintJob()`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Constructor.

Method Detail

addObject | () | method

`public function addObject(obj:IUIComponent, scaleType:String = matchWidth):void`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Adds a UIComponent object to the list of objects being printed. Call this method after the `start()` method returns. Each call to this method starts a new page, so you should format your objects in page-sized chunks. You can use the PrintDataGrid class to span a data grid across multiple pages.

Parameters

| `obj:IUIComponent` — The Object to be printed.
| `scaleType:String` (default = `matchWidth`)`` — The scaling technique to use to control how the object fits on one or more printed pages. Must be one of the constant values defined in the FlexPrintJobScaleType class.

See also

PrintDataGrid
FlexPrintJobScaleType

send | () | method |

`public function send():void`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Sends the added objects to the printer to start printing. Call this method after you have used the `addObject()` method to add the print pages.

start | () | method |

`public function start():Boolean`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Initializes the PrintJob object. Displays the operating system printer dialog to the user. Flex sets the `pageWidth` and `pageHeight` properties after this call returns.

Returns

| `Boolean` — `true` if the user clicks OK when the print dialog box appears, or `false` if the user clicks Cancel or if an error occurs.

Examples
FormPrintHeader.mxml

    <?xml version="1.0"?>
    <!--

      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    -->
    <!-- Custom control for the header area of the printed page. -->
    <s:VGroup name="FormPrintHeader"
            xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            width="60%"
            horizontalAlign="right" >

        <s:Label text="This is a placeholder for first page contents"/>

    </s:VGroup>

FormPrintFooter.mxml

    <?xml version="1.0"?>
    <!--

      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    -->
    <!-- Custom control for the footer area of the printed page. -->
    <s:VGroup name="FormPrintFooter"
            xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            width="60%"
            horizontalAlign="right" >

        <!-- Declare and initialize the product total variable. -->
        <fx:Script>
            <![CDATA[
                [Bindable]
                public var pTotal:Number = 0;
            ]]>
        </fx:Script>

        <s:Label text="Product Total: {pTotal}"/>

    </s:VGroup>

FormPrintView.mxml

    <?xml version="1.0"?>
    <!--

      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    -->
    <!-- Custom control to print the Halo DataGrid control on multiple pages. -->
    <s:VGroup name="FormPrintView"
            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="*">

        <fx: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();
                }
            ]]>
        </fx:Script>

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

        <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}" />

    </s:VGroup>

PrintDataGridExample.mxml

    <?xml version="1.0"?>
    <!--

      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    -->
    <!-- Main application to print a Halo DataGrid control on multiple pages. -->
    <s:Application name="PrintDataGridExample.mxml"
            xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx"
            initialize="initData();">

        <fx:Script>
            <![CDATA[

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

            // 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();
                    FlexGlobals.topLevelApplication.addElement(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.
                    FlexGlobals.topLevelApplication.removeElement(thePrintView);
                }
                // Send the job to the printer.
                printJob.send();
            }
            ]]>
        </fx:Script>

        <s:Panel title="DataGrid Printing Example"
                width="75%" height="75%"
                horizontalCenter="0" verticalCenter="0">
            <s:VGroup left="10" right="10" top="10" bottom="10">
                <mx:DataGrid id="myDataGrid" dataProvider="{dgProvider}">
                    <mx:columns>
                        <mx:DataGridColumn dataField="Index"/>
                        <mx:DataGridColumn dataField="Qty"/>
                    </mx:columns>
                </mx:DataGrid>

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

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

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

    </s:Application>

Wed Nov 15 2017, 09:50 AM +01:00