Tracked by Google

Home
Home
Printing and the PrintJob() Class - ActionScript 3.0
Written by Daniel   
Thursday, 02 April 2009 10:09

Printing is a relatively easy concept in most programs, you just hit the print button and it comes out of the chosen printer. However, it works a little differently in Flash, you must use the PrintJob class, which is a pretty difficult class to get used to for beginners.

The first concept to familiarise yourself with is Sprites. Sprites are new additions to ActionScript 3.0 and they are basically display object containers similar to a Movie Clip (you can put additional things inside them), however, they are solely code side concepts, you cannot make a Sprite design side onto the Flash stage, but you can generate one dynamically.

Okay, for this example, just create a button on the stage as you would normally, with the word "Print" on it, or a Print related symbol. Give it the instance name of btnPrint

We need to import all the classes we're going to be using in this example - 

import flash.display.SimpleButton;
i
mport flash.events.MouseEvent;
import flash.text.TextField;
import flash.printing.PrintJob;
import flash.display.Sprite;

We're going to be clicking on a button to activate the Print so we need SimpleButton and MouseEvent. I'll be showing you how to print a simple text field so we need the TextField Class, as well as (discussed above) PrintJob and Sprite.

Okay, first of all we need to make a variable with all our Print Text inside, this will make your code look cleaner and easier to understand.

var contentText = "This is what we're going to print, it's pretty easy to print, just ask me if you have any problems";

Next, is your event listener for your print button. Pretty simple utilisation of the MouseEvent class to generate the function printText.


btnPrint.addEventListener(MouseEvent.CLICK, printText);
function printText(Event:MouseEvent):void{

}

The next port of call is to understand the layout of your paper from a dimensions point of view - 50pixels is roughly 2cm on your standard A4 sheet of paper, so, if you start at 50x50 it looks normal, the best size for use on a portrait piece of A4 paper is 450x750; it took me a while and several wasted sheets of paper to discover this. So, to utilise this knowledge we need to place our TextField (named txtBody). This code can be placed inside the printText function. We then need to add our contentText variable into the TextField. Below is also a good example of some TextField properties which you may like to use in the future.

var txtBody:TextField= new TextField();
txtBody.x=50;
txtBody.y=50;
txtBody.width=450;
txtBody.height=750;
txtBody.multiline=true;
txtBody.wordWrap=true;
txtBody.text = contentText;

Okay, now we need to create our PrintJob (named myPrintJob) and our Sprite (named printSprite). Pretty easy creation of the new instance as you would normally.

var myPrintJob:PrintJob = new PrintJob();
var printSprite:Sprite = new Sprite();

Next, we need to add our TextField to the Sprite, which is pretty easily done. N.B. You can put anything static within a Sprite - graphics, images, text etc.

printSprite.addChild(txtBody);

Now comes the "action code" as I like to call it, all thats been done before is preparation, now comes the bit where we actually print something. So, starting the PrintJob can be done in a result Boolean variable (this allows errors to be processed as you can attach an "if conditional" statement to it which you'll also see below).

var result:Boolean=myPrintJob.start();
if (result) {
    myPrintJob.addPage(printSprite);
    myPrintJob.send();
    trace("sent");
} else {
    trace("cancelled");
}

And there you have it. A complete print job. Below is the full code.

import flash.display.SimpleButton;
i
mport flash.events.MouseEvent;
import flash.text.TextField;
import flash.printing.PrintJob;
import flash.display.Sprite;

var contentText = "This is what we're going to print, it's pretty easy to print, just ask me if you have any problems";

btnPrint.addEventListener(MouseEvent.CLICK, printText);

function printText(Event:MouseEvent):void{

    var txtBody:TextField= new TextField();
    txtBody.x=50;
    txtBody.y=50;
    txtBody.width=450;
    txtBody.height=750;
    txtBody.multiline=true;
    txtBody.wordWrap=true;
    txtBody.text = contentText;

    var myPrintJob:PrintJob = new PrintJob();
    var printSprite:Sprite = new Sprite(); 

    printSprite.addChild(txtBody);

    var result:Boolean=myPrintJob.start();
    if (result) {
        myPrintJob.addPage(printSprite);
        myPrintJob.send();
        trace("sent");
    } else {
        trace("cancelled");
    }


 

Last Updated on Monday, 27 April 2009 12:31
 
home search