`

Monday, September 20, 2010

Page print with AS 3.0

In this  tutorial, let’s see how we can print the content  using the PrintJob class
  • First Create a new flash file and save it as PrintTest.fla
  • Create the content that you want to print . 
  • Convert the whole content into a movie clip and give it an instance name of “page_mc”. 
Note :you must have a moveclip to print (i.e. if you want to print a text field, put it in a movieClip
  • And create the print button. Give it an instance of "printBtn_mc".
  • Finally create the “actions” layer. Open the actions panel.
    Type the following code.
|| ----------------------------------------------------------------------- ||


import flash.events.MouseEvent;
import flash.printing.PrintJob;
import flash.geom.Rectangle;

//  on Print function

function onPrint(event:MouseEvent):void{
    //a variable to hold new print job
    var pj:PrintJob = new PrintJob();
   
    // display Print dialog box, but don’t start the print job unless .start() returns successful
    var result:Boolean = pj.start()
   
    if(result){
        // add specified page to print job
        try{
            var rect:Rectangle = new Rectangle(page_mc.x, page_mc.y,page_mc.width, page_mc.height)
            pj.addPage(page_mc, rect, null, 0);
        }
        // in case an error pops up (i.e. printer not connected). warn the user to try again
        catch(event:Error){
            trace("unable to Print");
        }
    }
    // calls to addPage() was successful. You should always check for successful
    pj.addPage(page_mc)
    // Finally  calling send().
    pj.send()
}

printBtn_mc.addEventListener(MouseEvent.CLICK, onPrint);


|| ----------------------------------------------------------------------- ||

Preview

Download .zip

No comments: