Monday, January 24, 2011

How to print a MovieClip/Sprite/Image in landscape mode:

Below is the code to print a MovieClip/Sprite/Image in landscape mode.  The minimum requirement is Flash Player 9 or Air 1.0.




import flash.printing.PrintJob; 
import flash.printing.PrintJobOrientation;
/**
 * This function prints a sprite to landscape mode.
 * 
 * @param     image     the sprite to be printed. 
 */
function printLandscape( image:Sprite ) : void {
    var realW:Number = clip.width;
    var realH:Number = clip.height;
    var orgX:Number = clip.x;
    var orgY:Number = clip.y;
    var pj:PrintJob = new PrintJob();
    var options:PrintJobOptions = new PrintJobOptions();
    options.printAsBitmap = true;
    var pageCount:Number = 0;
   
    if ( !pj.start() ) return;
   
    clip.x = 0;
    clip.y = 0;
   
    var cscaleX:Number, cscaleY:Number;
   
    if ( pj.orientation.toLowerCase() != "landscape" ) {
        clip.rotation = 90;
        clip.x = clip.width;
        cscaleX = pj.pageWidth / realH;
        cscaleY = pj.pageHeight / realW;
    }
    else {
        cscaleX = pj.pageWidth / realW;
        cscaleY = pj.pageHeight / realH;
    }
   
    clip.scaleX = clip.scaleY = Math.min( cscaleX, cscaleY );
    if ( pj.addPage(clip, new Rectangle(0, 0, realW, realH), options) ) 

       pageCount++;
   
    if (pageCount > 0) 

       pj.send();
    

    clip.scaleX = clip.scaleY = 1;
    clip.rotation = 0;
    clip.x = orgX;
    clip.y = orgY;
    pj=null;
}

Related Links:

4 comments:

  1. OH MY GOD YOU ARE A GOD!!!
    Can I have your address so I can mail you my first born... or a steak - your choice my lord.

    ReplyDelete
  2. probably have to change the first parameter from image:Sprite to clip:Sprite, as the image wasn't mentioned in any of the method, and clip wasn't in any of the passed parameter.

    ReplyDelete
  3. Unfortunately I recieve this message:
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::MouseEvent@3ce20d65f71 to flash.display.Sprite
    What does it mean?

    ReplyDelete