AS3 print with button

861 views Asked by At

When I try to print my stage it results wrongly rotated. I tried to fix it, I googled 150 different solutions pages at least, without any luck. I am not the programmer I try to be. Anyway it should work but it doesn't. Can you help me please?

I tried to do that: when I click "print" button, I would like to save my page(sprit) as a pdf file but per default settings the page must be landscape oriented. Furthermore, there is a drawing (I use someone's code which shared in web) in stage but I don't want save the whole scene (for example buttons). I want to save just the drawing. Thanks for your help.

İ used this (didn't work at all. even drawing didn't work.)

btnPrint.addEventListener(MouseEvent.CLICK, onPrintClick);
function onPrintClick(event:MouseEvent):void{
    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 myPrintjob:PrintJob = new PrintJob();   
    myPrintjob.start();
    var printArea = new Rectangle( 23, 65, 650, 500 ); 
    myPrintJob.addPage( s, printArea );
    myPrintJob.send();
}

I tried also this approach (draw and print worked but orientation was wrong):

btnPrint.addEventListener(MouseEvent.CLICK, onPrintClick);
function onPrintClick(event:Event)void{
    var myPrintjob:PrintJob = newPrintJob;
    myPrintjob.start();
    myPrintjob.addPage(this)
    myPrintjob.send;
} 

I also tried some code I don't remember. It was like oriented.landscape and rotation 90, or something like that. It didn't work too. Probably I didn't make it.

2

There are 2 answers

2
Cadin On

If you're publishing for AIR you can change page orientation using PrintJob.orientation.
You need to set that before calling start(). In your code example it would look something like this:

btnPrint.addEventListener(MouseEvent.CLICK, onPrintClick);
function onPrintClick(event:Event)void{
    var myPrintjob:PrintJob = new PrintJob();
    myPrintJob.orientation = PrintJobOrientation.LANDSCAPE; 
    myPrintjob.start();
    myPrintjob.addPage(this)
    myPrintjob.send();
} 

Unfortunately this property is not settable if building for Flash Player.

0
emrah yalcin On

I used these codes and they did the job. Thanks for your efforts.

function onPrintClick(event:Event):void{
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 pj:PrintJob = new PrintJob();   
pj.start();
pj.orientation = PrintJobOrientation.LANDSCAPE;
var printArea = new Rectangle( Cw, Ch, 650, 500 );
pj.addPage(this);    
pj.send();
}