Printing for the First Time from Flex


In this task you will print for the first time from Flex. In this case you will focus on the steps needed to enable printing; what you print will be of lesser importance. You will print a VBox container that contains a Label and a Button control.

1.

Choose File > New > Flex Project. Select Basic and then click Next.

You do not need FDS or ColdFusion support for this project.

2.

Make the project name flex2tfs_Printing and use the folder flex2tfs/Lesson23/printing/start. Click Next.

The directory contains starter files you will use for your exploration of drag and drop.

3.

Click the Browse button next to the Main application file option and select the file PrintingTask1.mxml. Click OK and then click Finish.

You are creating a new project because some of the work in this lesson will not be directly involved with any of the three applications you are working on from the FlexGrocer site.

Notice that this file has a VBox that contains a Label and a Button control. The Button has a click event that calls a function named doPrint(). The skeleton of the doPrint() function is supplied in a script block.

4.

At the top of the script block, import the mx.printing.FlexPrintJob class.

Remember that you could have skipped this step; when you used the class for data typing a variable, it would have been automatically imported for you.

5.

As the first line of code in the function, create a variable local to the function named pj, data typed as FlexPrintJob, and set it equal to a new FlexPrintJob.

var pj:FlexPrintJob=new FlexPrintJob(); 


This instance of the FlexPrintJob class will be where all the printing will center around.

6.

On the pj FlexPrintJob object instance, invoke the start() method.

pj.start(); 


This initializes the FlexPrintJob object and causes the underlying operating system to display a print dialog box.

7.

On the pj object, invoke the addObject() method passing the id of the VBox, printContainer, as a parameter.

pj.addObject(printContainer); 


This adds the VBox to the list of objects to be printed.

8.

On the pj object, invoke the send() method.

pj.send(); 


This sends the added objects to the printer to start printing.

Tip

The send() method is synchronous, so the code that follows it can assume that the call completed successfully.

9.

Ensure that your function appears as follows:

private function doPrint():void{    var pj:FlexPrintJob=new FlexPrintJob();    pj.start();    pj.addObject(printContainer);    pj.send(); } 


Between the calls to the start() and send() methods, a print job is spooled to the underlying operating system. You should limit the code between these calls to only print-specific methods. For instance, there should not be user interaction between those two methods.

10.

Run the application. Click the Button labeled Print Page in the Flex application to print the contents of the VBox.

You have now implemented the simplest print job in Flex.

11.

Run the application again. Click the print Button and this time cancel the print job. (Depending on your environment, you might have to be very quick to cancel the print job.)

Notice that the screen clears when you do this. You need to gracefully handle when the user cancels the print job.

12.

In the function, wrap the invocation of the start() method in an if statement. Check to see whether the value returned from the invocation is not equal to true. If the condition is true, simply return from the function. Your code should appear as follows:

private function doPrint():void{    var pj:FlexPrintJob=new FlexPrintJob();    if(pj.start() != true){       return;    }    pj.addObject(printContainer);    pj.send(); } 


This causes Flex to gracefully exit back to the application page if the user cancels the print job.

13.

Run the application again and cancel the print job after clicking the print button.

You should see that the application remains visible this time.




Adobe Flex 2.Training from the Source
Adobe Flex 2: Training from the Source
ISBN: 032142316X
EAN: 2147483647
Year: 2006
Pages: 225

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net