Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Flex print support


May 25, 2021 Flex


Table of contents


Flex provides a special class FlexPrintJob to print flex objects.

  • FlexPrintJob can be used to print one or more Flex objects, such as Form or VBox containers.

  • FlexPrintJob prints objects and all the objects they contain.

  • An object can be all or part of the displayed interface.

  • Objects can be components that format the data used for printing.

  • The FlexPrintJob class allows you to scale the output to fit the page.

  • The FlexPrintJob class automatically uses multiple pages to print objects that are not suitable for a single page.

  • The FlexPrintJob class enables the operating system to display a "print" dialog box. /b10> If there are no user actions, you cannot print.

Prepare and send a print job

Print output by preparing and sending print jobs. Let's create an instance of the FlexPrintJob class

var printJob:FlexPrintJob = new FlexPrintJob();

Start the print job

printJob.start(); 

Flex causes the operating system to display a Print dialog box. Add one or more objects to the print job and specify how to scale them

printJob.addObject(myObject, FlexPrintJobScaleType.MATCH_WIDTH); 

Each object starts with a new page. Send a print job to the printer

printJob.send(); 

Print an example

Steps Describe
1 As described in the Flex - Create Applications section, create a project called HelloWorld under package com.tutorialspoint.client.
2 Modify HelloWorld.mxml, as described below. /b10> Keep the rest of the file unchanged.
3 Compile and run the application to ensure that the business logic works as required.

The following is the contents of the modified mxml file 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>

When all the changes are ready, let's compile and run the application in normal mode, as in the Flex - Create Application chapter. If all goes well with your application, this will result in the following results:

Flex print support

Click the Print Me button and you can see the printout of the data grid as follows.

Flex print support