Using the PrintJob Class


The PrintJob class is a built-in Flash class that gives the programmer control over what can be printed in a SWF file as well as how it's printed. To use the PrintJob class, a new instance of the class must be created.

   var myPrintJob:PrintJob = new PrintJob();


With this script, a new instance of the PrintJob class is created and referred to as myPrintJob.

To tell the PrintJob object what content to print, you must use the addPage() method of the PrintJob class. We'll get to this soon. Before you can use the addPage() method to add all printable content to the print job, however, you must first call the start() method of the PrintJob class:

   var myPrintJob:PrintJob = new PrintJob();    var result:Boolean = myPrintJob.start();


The first line of this script creates the instance of the PrintJob class. The second line invokes the start() method. The moment that the start() method is called, a pop-up window (specific to the operating system) asks whether the user wants to proceed with printing. If the user selects Print, the Boolean value true is returned and stored as the value of the variable result. If the user doesn't have a printer installed or cancels the print pop-up window, the Boolean value false is returned. This feature allows you to program an application to react one way if the user successfully initializes the print option, and another if the user cancels the print request or doesn't have a printer. For example:

   var myPrintJob:PrintJob = new PrintJob();    var result:Boolean = myPrintJob.start();    if (result) {      //Successfully initialized print action      //Add pages to print here    } else {      //User does not have printer or user canceled print action    }


Note

After the start() method is called but before the user responds to the popup window, Flash is paused and no frames are executed. All animations and code halt until the user responds.


If the value of result is true, the user has a printer and has chosen the Print option. It's then time to use the addPage() method of the PrintJob class to add the printable content. The syntax for invoking the addPage() method looks like this:

   myPrintJob.addPage(target, printArea, options, frameNumber);


The addPage() method has four parameters:

  • target. This option defines the Timeline where the page lives that you want to print. If this parameter is entered as a number, it's interpreted as pointing to a level of the movie. If the value entered is a string, it points to a movie clip.

  • printArea. This parameter expects an object with four properties: xMin, xMax, yMin, and yMax. These properties together form a rectangle determining the printable area of the target. All of these measurements are relative to the registration point of the target. For example, if xMin has a value of -300, the left border of the printable area is 300 pixels to the left of the registration point of the target. By default, leaving the printArea parameter blank prints the entire dimensions of the movie clip (or what can fit on the printed page).

  • options. This setting specifies whether the page should be printed as a bitmap image or as vector graphics. The parameter value needs to be an object with a single property, printAsBitmap. This property has a Boolean value. If true, the page is printed as a bitmap. If false, it's printed as vector graphics. By default, leaving the options parameter blank prints the page as vector graphics. By printing as a bitmap you can print movies that maintain their transparencies and color effects, as shown onscreen.

  • frameNumber. This parameter is a number specifying the frame within the target to be printed. It works only with frame numbers, not frame labels. By default, if this parameter is blank, the currently displayed frame of the target is printed.

All the parameters of the addPage() method are optional except target.

Let's look at some examples of this method in use. The following script creates a new PrintJob class instance, starts the print request, and adds a page to be printed. The currently displayed frame of the myClip_mc movie clip instance will be printed as vector graphics.

   var myPrintJob:PrintJob = new PrintJob();    var result:Boolean = myPrintJob.start();    if (result) {      myPrint.addPage("myClip_mc");    } else {      //User does not have printer or user canceled print action    }


To use the default value of a parameter, enter null. The following line adds Frame 5 of the myClip_mc movie clip instance to the PrintJob class instance, and specifies that it should be printed as a bitmap:

   myPrint.addPage("myClip_mc", null, {printAsBitmap:true}, 5);


To specify the dimensions of a movie clip to be printed, you must define the printArea (second parameter) using an object, as shown here:

   myPrintJob.addPage(0, {xMin:30, xMax:250, yMin:27, yMax:300});


The target is level 0. This addPage() method instructs Flash to print all content in level 0 on the current frame that's shown between the x positions of 30 and 250, and the y positions of 27 and 300. Only content found within these dimensions will be printed.

You can add pages from various Timelines to a single PrintJob instance, allowing the user to print content from those various Timelines from a single Print dialog box:

   myPrintJob.addPage("invitation_mc", null, {printAsBitmap:true}, 2);    myPrintJob.addPage("map_mc", null, {printAsBitmap:false}, 1);    myPrintJob.addPage(1, null, {printAsBitmap:true}, null);    myPrintJob.addPage("guestList_mc", null, {printAsBitmap:true}, 4);


To add all frames of a Timeline to a print job, use a looping statement:

   for(i = 1; i <= myMovieClip_mc._totalframes; ++1){      myPrintJob.addPage("myMovieClip_mc", null, null, i);    }


With each loop, a page is added to the print job. The current value of i specifies by which frame in the myMovieClip_mc instance to print for that page. This loop continues until i is greater than the number of frames in the instance.

Note

Remember that a Timeline needn't be visible to add frames from that Timeline to a print job. This feature allows you to create hidden content in your movie that might only be appropriate for printing purposes, such as coupons, instructions, or maps that don't fit into your project's overall design.


After all pages have been added to a PrintJob instance, you invoke the send() method of the PrintJob class to start the printing.

   myPrintJob.send();


After you're done with the instance of the PrintJob class, you should delete it.

   delete myPrintJob;


Tip

Don't leave instances around that no longer have any use; that's a waste of memory.


A complete script for creating a print job will look something like this:

   var myPrintJob:PrintJob = new PrintJob();    var result:Boolean = myPrintJob.start();    if (result) {      myPrintJob.addPage("invitation_mc", null, {printAsBitmap:true}, 2);      myPrintJob.addPage("map_mc", null, {printAsBitmap:false}, 1);      myPrintJob.addPage(1, null, {printAsBitmap:true}, null);      myPrintJob.addPage("guestList_mc", null, {printAsBitmap:true}, 4);      for(i = 1; i <= myMovieClip_mc._totalframes; ++1){        myPrintJob.addPage("myMovieClip_mc", null, null, i);      }      myPrintJob.send();      delete myPrintJob;    } else {      //User does not have printer or user canceled print action    }


In the following exercise, you'll dynamically select part of an image and print it.

1.

Open PrintArea1.fla in the Lesson17/Start folder.

There are four layers on the main Timeline: Actions, PrintArea Box, Interface, and Animals. The Actions layer is where you'll add the ActionScript for this exercise. The PrintArea Box layer contains a movie clip instance named box_mc. The Interface layer contains the main interface graphics for this application, and the Animals layer contains six frames of animal pictures.

When this exercise is complete, you can navigate images using the Left arrow and Right arrow keys on the keyboard. Clicking an image and dragging down and to the right draws a box on top of the image. As soon as the user releases the mouse button, a print command is sent to print the area of the image that was just selected. The box_mc movie clip is resized as the mouse moves, to give the impression that the user is selecting part of the image.

2.

With the Actions panel open, select Frame 1 of the Actions layer and add the following script:

   stop();    box_mc._visible = false;


The first action prevents the Timeline from moving forward until we tell it to do so. The second line makes the box_mc movie clip instance invisible initially. We only want it to be shown if the user has pressed the left mouse button and dragged to create a selection.

3.

Add the following variable to track the state of the mouse:

   var down:Boolean = false;


The movie initializes with the mouse button up. When the left mouse button is pressed, the down variable changes to TRue.

4.

Add the following four variables to track the print area dimensions:

   var xMin:Number;    var xMax:Number;    var yMin:Number;    var yMax:Number;


To specify the area of a target to print, you specify four values: xMin, xMax, yMin, and yMax. These values are declared here with no value. When the mouse button is pressed, the xMin and yMin values are set. As the mouse moves, the xMax and yMax values are set.

5.

Add the following onMouseDown event to handle initializing the selection:

   this.onMouseDown = function() {      down = true;      xMin = _xmouse;      yMin = _ymouse;      box_mc._x = xMin;      box_mc._y = yMin;    };


When the left mouse button is pressed, the down variable's value is set to true. The minimum selection values, xMin and yMin, are set based on the mouse position when the event occurs. These two values are immediately used to position the box_mc movie clip.

6.

To handle scaling the selection as the mouse moves, add the following script:

   this.onMouseMove = function() {      updateAfterEvent();      if (down) {        box_mc._visible = true;        xMax = _xmouse;        yMax = _ymouse;        box_mc._width = xMax - xMin;        box_mc._height = yMax - yMin;      }    };


The updateAfterEvent() function is a built-in function in Flash. It tells Flash to redraw graphics onscreen after the mouse moves, rather than waiting until the end of the current frame. This technique gives smoother results to objects that change as the mouse moves.

Every time the mouse moves, a conditional statement is evaluated. It states that if down is true (as it is when the mouse button is pressed), the box_mc instance is made visible. Next, the values of xMax and yMax are set based on the current position of the mouse. Finally, the box_mc movie clip instance's dimensions are set based on the distances between the minimum values (captured when the mouse button was pressed) and the maximum values (which change as the user moves the mouse around). The end result is that the box_mc movie clip instance scales as the mouse moves to emulate selecting.

7.

Add the following script to capture the onMouseUp event:

   this.onMouseUp = function() {      down = false;      box_mc._visible = false;      printImage();    };


When the mouse button is released, the down variable is set back to false, and the box_mc movie clip becomes invisible again. Then the printImage() function is called; this function uses the dimension variables from the selection process to print an area of the image. Let's create that function next.

8.

At the end of the current script, add the following function definition:

   function printImage() {      var myPrintJob:PrintJob = new PrintJob();      var result:Boolean = myPrintJob.start();      if (result) {        myPrintJob.addPage(0, {xMin:xMin, xMax:xMax, yMin:yMin, ¬          yMax:yMax}, {printAsBitmap:true}, _currentframe);        myPrintJob.send();      }      delete myPrintJob;    }


This function is called after the user has clicked, dragged, and released the mouse button. A new instance of the PrintJob class is created and stored as myPrintJob; then the start() method is called, the result of which is stored as the value of the result variable.

If the value of result is true, the user has a printer and has opted to continue the printing process. The addPage() method is then called, and the target Timeline to print is set as level 0. The second parameter passes in an object whose properties are the dimensions that were gathered during the selection process. The third parameter specifies that the image should be printed as a bitmap. The fourth parameter specifies that the frame to print is the current frame where the main Timeline resides. Thus, if the user has navigated to Frame 6, a portion of the hippo graphic, as defined by the selection dimensions, will print.

After the addPage() action, the function sends the added page to the printer by using the send() method. When the send() method is called, the Flash Player sends all the added pages (a single page in this case) to the printer to be printed.

The last line in this function deletes the instance of the PrintJob class because we're done with it.

The last thing we need to do is add the script to enable the user to navigate the animal images.

9.

At the end of the current script, add the following function definition:

   this.onKeyDown = function(){      if(Key.isDown(Key.RIGHT)){        nextFrame();      }else if(Key.isDown(Key.LEFT)){        prevFrame();      }    }    Key.addListener(this);


You should be familiar with how this script works. When the onKeyDown event occurs, a conditional statement checks whether the key pressed down is the Left arrow or Right arrow key. If the Left arrow key is pressed, the main Timeline moves to the next frame. If the Right arrow key is pressed, the main Timeline moves to the previous frame. If any other key is pressed, nothing happens.

Let's test our work.

10.

Select Control > Test Movie. Select a portion of the image and then release the mouse button to print it.

After you select a portion of the image, a print window should pop up, opened by your operating system. If you proceed with the print job, the selected area of the image will be printed.

Note

The ActionScript that we added in this exercise supports selecting a portion of the image by clicking and dragging down and to the right. Using more advanced ActionScript, we could have supported selecting in any direction.

11.

Close the test movie and save your work as PrintArea2.fla.

You have successfully created a simple application that uses the PrintJob class. You can print a specific area of an image with this application.




Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

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