1
votes

I cannot print the stage:

btn.addEventListener(MouseEvent.CLICK, printFunction);

function printFunction(event:MouseEvent):void
{
    var myPrintJob:PrintJob = new PrintJob();
    myPrintJob.addPage(0);
    myPrintJob.send();

}

it gives me a compile error:

1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:Sprite.

I've also tried:

myPrintJob.addPage(Sprite(0));

there's no compile error; but when I click the print button, there's no print dialog and the output section in Flash gives me this error:

TypeError: Error #1034: Type Coercion failed: cannot convert 0 to flash.display.Sprite. at Untitled_fla::MainTimeline/printFunction()

1
That's not where your issue is. How is btn declared?Dave

1 Answers

3
votes

Printjob's addPage method is expecting a Sprite as first parameter.

What are you trying to achive by passing 0?

If you want a blank page, try:

var myPrintJob:PrintJob = new PrintJob();
myPrintJob.start(); /*Initiates the printing process for the operating system, calling the print dialog box for the user, and populates the read-only properties of the print job.*/
myPrintJob.addPage( new Sprite() );
myPrintJob.send();

Another example with a red square:

var s:Sprite = new Sprite();
s.graphics.beginFill(0xFF0000);
s.graphics.drawRect(0, 0, 80, 80);
s.graphics.endFill();

var myPrintJob:PrintJob = new PrintJob();
myPrintJob.start(); /*Initiates the printing process for the operating system, calling the print dialog box for the user, and populates the read-only properties of the print job.*/
myPrintJob.addPage( s );
myPrintJob.send();

More info here.

To print a part of stage, you could:

1) Wrap everything you want to print in a sprite and pass that sprite to addPage().

or

2) Use BitmapData

 var bd :BitmapData = new BitmapData(stage.width, stage.height, false);
 bd.draw(stage);
 var b:Bitmap = new Bitmap (bd);
 var s:Sprite = new Sprite();
 s.addChild(b);

 var printArea = new Rectangle( 0, 0, 200, 200 ); // The area you want to crop

 myPrintJob.addPage( s, printArea );