Saturday, June 4, 2011

Printing in AS3: Part 1

In this tutorial we will learn about printing in Actionscript 3.

The class that is responsible for printing in AS3 is PrintJob. It is used to make whatever is displayed on the screen ready for printing and send the data to the user's printing device.

When working with printing, it is important to always remember that displaying images on screen and on paper are different things. For example, screen resolution is not the same thing as printer resolution. The content on the screen is dynamic and can change (ex. animations), while whatever is on paper is static and can't move.

These things, even though they are obvious, should always be kept in mind when working with printing. In the next few tutorials, which will be dedicated to the PrintJob class, we will use some keywords that might need some explanation.

Spooler - this is an OS component or a driver software that is used to store the data that is sent to be printed, and then feeds this data to the printer when it needs to.

Page orientation - the rotation of the image on the paper. It can be either landscape (horizontal) or portrait (vertical).

Let's get started with this PrintJob class.

To successfully perform a printing operation, you need to call 4 methods one after another, and those are:

  1. new PrintJob() - create a new PrintJob object.
  2. PrintJob.start() - start a printing process in the OS. This opens a dialog print window, where user can set their settings, such as printing quality, orientation, etc.
  3. PrintJob.addPage() - contains information about the print job as parameters, including the Sprite object that needs to be printed, a Rectangle object representing the printing area, a PrintJobOptions object and a frameNum value, which you can set to print a specific frame in the specified MovieClip.
  4. PrintJob.send() - sends the page to the printer.

Here's the simplest possible example of starting a print job (assuming mySprite is a MovieClip on stage):

var myPrintJob:PrintJob = new PrintJob();

This will open a new dialog window, where the user can change printing settings.

Next time we'll go deeper into printing and learn more.

Thanks for reading!


Printing in AS3: Part 2
Printing in AS3: Part 3
Printing in AS3: Part 4


Laurence E. Moore said...

Hola -
I am getting this error:
Error: Error #2057: The page could not be added to the print job.
at Error$/throwError()
at flash.printing::PrintJob/addPage()
at printing002_fla::MainTimeline/frame1()

Its seems this is common on the web - with no solution - what say u?

Awesome Blog

Kirill Poletaev said...

Are you sure the object you're trying to print is a MovieClip?

Post a Comment