Messing with the PrintJob Class

We teamed up with SiteGround
To bring you up to 65% off web hosting, plus free access to the entire SitePoint Premium library (worth $99). Get SiteGround + SitePoint Premium Now

Its the same with anything, until you need to use the functionality of a given class or object in a project, you never delve into it unless you’re an ardent explorer type person!. I found the need to quickly explore the printing capabilities of Flash MX 2004 and was pleasantly surprised.

In Flash MX 2004, the new PrintJob class is much more powerful than the print() and printAsBitmap() functions in previous versions, allowing you to print individual MovieClips, sections of MovieClips (using print area parameters) or entire levels with simplistic ease.

In the example outlined here (source code found here), I needed to quickly print out a given number of MovieClips from the stage (example shown below).

ScreenShot of Stage

I created the following function to pass in a simple array of MovieClips allowing me to add multiple pages to the print queue.

function PrintWhat(WhatToPrint:Array) {
var PrintQueue = new PrintJob();
var PrintStart:Boolean = PrintQueue.start();
if (PrintStart) {
for (i=0; i<=WhatToPrint.length; i++) { PrintQueue.addPage(WhatToPrint);
}
PrintQueue.send();
}
}
//Prints Specific MovieClips
PrintWhat([MCPrint, MCPrint002,MCPrint003]);
//Prints a Level
//PrintWhat([0]);

The PrintWhat() function accepts the array of MovieClips or levels as a parameter, start a new print job using the PrintJob object, call the start() method which then triggers the print dialog. The start() method returns a Boolean true or false value based on whether the user clicks ‘OK’ or ‘Cancel’ in the print dialog box, and the inner condition when PrintStart is true is triggered or stepped over.

If the user chose to print and clicks ‘OK’, the incoming WhatToPrint array is parsed, and pages are added to the print queue using the addPage() method and the job is sent to the printer using the send() method.

Note: You can pass extra parameters for printing portions of MovieClips, but I