as3实现打印功能:
主要用到PrintJob类中的start()、addPage(mc:sprite,rect,option)、send()三个方法。
单页打印代码
[java]
package{
import flash.display.Sprite;
import flash.printing.PrintJob;
import flash.printing.PrintJobOptions;
import flash.printing.PrintJobOrientation;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
public class BasicPrintExample extends Sprite{
private var myPrintJob:PrintJob = new PrintJob();
private var mySprite:Sprite = new Sprite();
private var options:PrintJobOptions = new PrintJobOptions();
private var rect1:Rectangle = new Rectangle(0,0,400,200);
public function BasicPrintExample(){
addChild(mySprite);
mySprite.addChild(mc);
btn.addEventListener(MouseEvent.CLICK, btnClick);
}
private function btnClick(e){
printJob();
}
private function printJob(){
options.printAsBitmap = true;
myPrintJob.start();
myPrintJob.addPage(mySprite,rect1,options);
myPrintJob.send();
}
}
}
package{
import flash.display.Sprite;
import flash.printing.PrintJob;
import flash.printing.PrintJobOptions;
import flash.printing.PrintJobOrientation;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
public class BasicPrintExample extends Sprite{
private var myPrintJob:PrintJob = new PrintJob();
private var mySprite:Sprite = new Sprite();
private var options:PrintJobOptions = new PrintJobOptions();
private var rect1:Rectangle = new Rectangle(0,0,400,200);
public function BasicPrintExample(){
addChild(mySprite);
mySprite.addChild(mc);
btn.addEventListener(MouseEvent.CLICK, btnClick);
}
private function btnClick(e){
printJob();
}
private function printJob(){
options.printAsBitmap = true;
myPrintJob.start();
myPrintJob.addPage(mySprite,rect1,options);
myPrintJob.send();
}
}
}多页打印代码
[java]
package {
//多页打印类
import flash.display.MovieClip;
import flash.printing.PrintJob;
import flash.printing.PrintJobOrientation;
import flash.display.Stage;
import flash.display.Sprite;
import flash.text.TextField;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
public class PrintMultiplePages extends MovieClip {
private var sheet1:Sprite;
private var sheet2:Sprite;
private var sheet3:Sprite;
public function PrintMultiplePages() {
// constructor code
init();
btn.addEventListener(MouseEvent.CLICK, btnClick);
}
private function btnClick(e):void{
printPages();//打印
}
private function init():void{
sheet1 = new Sprite();
createSheet(sheet1, "Once upon a time...",{x:10, y:50, width:80, height:130});
sheet2 = new Sprite();
createSheet(sheet2, "There was a great story to tell, and it ended quickly.\n\nThe end.", null);
sheet3 = new Sprite();
createSheet(sheet3, "你好,打印第三页!",null);