Tuesday, June 7, 2011

Printing in AS3: Part 4

Today we continue learning about printing.

Last time we found out how to set a print area of a page by passing a Rectangle object as a parameter to addPage() method.

The Rectangle object's dimensions (width and height) are set in pixels. The printer, however, uses points as measuring units instead of pixels. Points have a fixed physical size - 1/72 of an inch, while a pixel on the screen depends on the resolution of that screen.

The transformation multiplier between pixels and points depends on the printer's settings and the Sprite object's scale.

A non-scaled Sprite object that has a width of 72 pixels will be printed with 1 inch in width. One point will equal one pixel, no matter what the screen resolution is.

Here are some of the relations that might come in handy:

  • 1 point = 20 tweeps = 1/72 inches
  • 1 inch = 72 points = 1440 tweeps
  • 1 cm = 567 tweeps

If you need to change the scale of your Sprite object that you're going to print, do it before you call the addPage() method on it.

Note that printArea property and the Sprite object's scale are not connected. So, if you set your print area to 100 x 100 pixels, it is going to print 10000 pixels total. If you re-scale your Sprite object, it will print the same 10000 pixels, but the Sprite object is printed in its rescaled appearance.

You can send and read orientation of the page using the orientation property of your PrintJob object.

The values of this orientation property are static properties of PrintJobOrientation class. To set the page's orientation to horizontal, set it to:

myPrintJob.orientation = PrintJobOrientation.LANDSCAPE;

To set it to vertical:

myPrintJob.orientation = PrintJobOrientation.PORTRAIT;

Thanks for reading!


Printing in AS3: Part 1
Printing in AS3: Part 2
Printing in AS3: Part 3


Anonymous said...

Thanks for this tutorial but I have 1 problem, when I use :

myPrintJob.orientation = PrintJobOrientation.LANDSCAPE;

I get an error: Property is read-only.

Any suggestions ?

Kirill Poletaev said...

The orientation property is: Flash Player 9 - read only, AIR 1.0 - read only, AIR 2 - read-write.

CoDo said...

I want to print a result form (Sprite with textfields) but I always get this error:
Filter will not render. The DisplayObject's filtered dimensions (20218, 10219) are too large to be drawn.

Do I need to set the width and height?
And if, what values?


Kirill Poletaev said...

Are you using any filters?

I might be wrong about this, but as far as I know, flash/air won't print filters.

The solution for this would be first 'capturing' whatever you want to print and drawing it in a BitmapData object, then printing that object.

CoDo said...

Are TextFormat and TextFieldAutoSize filters?

CoDo said...

Even when I only create a sprite of 100x100 is gives me the error???
I can show you my coed if you like??

Kirill Poletaev said...

Hmm, yeah give me the code, I'll take a look into it.

CoDo said...

here it is:

CoDo said...

The Main.vo.whatever are stored strings

Post a Comment