AS3使用PrintJob实现单页或多页打印

来源:互联网 发布:西南交大网络教学 编辑:程序博客网 时间:2024/05/21 09:22

as3实现打印功能:

主要用到PrintJob类中的start()、addPage(mc:sprite,rect,option)、send()三个方法。

单页打印代码

[java] view plaincopy
  1. package{  
  2.       
  3.     import flash.display.Sprite;  
  4.     import flash.printing.PrintJob;  
  5.     import flash.printing.PrintJobOptions;  
  6.     import flash.printing.PrintJobOrientation;  
  7.     import flash.geom.Rectangle;  
  8.     import flash.events.MouseEvent;  
  9.       
  10.     public class BasicPrintExample extends Sprite{  
  11.           
  12.         private var myPrintJob:PrintJob = new PrintJob();  
  13.         private var mySprite:Sprite = new Sprite();  
  14.         private var options:PrintJobOptions = new PrintJobOptions();  
  15.         private var rect1:Rectangle = new Rectangle(0,0,400,200);  
  16.           
  17.         public function BasicPrintExample(){  
  18.               
  19.             addChild(mySprite);  
  20.             mySprite.addChild(mc);  
  21.               
  22.             btn.addEventListener(MouseEvent.CLICK, btnClick);  
  23.         }  
  24.           
  25.         private function btnClick(e){  
  26.             printJob();  
  27.         }  
  28.           
  29.         private function printJob(){  
  30.             options.printAsBitmap = true;  
  31.             myPrintJob.start();  
  32.             myPrintJob.addPage(mySprite,rect1,options);  
  33.             myPrintJob.send();  
  34.         }  
  35.           
  36.     }  
  37. }  

多页打印代码

[java] view plaincopy
  1. package  {  
  2.     //多页打印类  
  3.     import flash.display.MovieClip;  
  4.     import flash.printing.PrintJob;  
  5.     import flash.printing.PrintJobOrientation;  
  6.     import flash.display.Stage;  
  7.     import flash.display.Sprite;  
  8.     import flash.text.TextField;  
  9.     import flash.geom.Rectangle;  
  10.     import flash.events.MouseEvent;  
  11.       
  12.     public class PrintMultiplePages extends MovieClip {  
  13.   
  14.         private var sheet1:Sprite;  
  15.         private var sheet2:Sprite;  
  16.         private var sheet3:Sprite;  
  17.   
  18.         public function PrintMultiplePages() {  
  19.             // constructor code  
  20.             init();  
  21.               
  22.             btn.addEventListener(MouseEvent.CLICK, btnClick);  
  23.         }  
  24.           
  25.         private function btnClick(e):void{  
  26.             printPages();//打印  
  27.         }  
  28.           
  29.         private function init():void{  
  30.             sheet1 = new Sprite();  
  31.             createSheet(sheet1, "Once upon a time...",{x:10, y:50, width:80, height:130});  
  32.             sheet2 = new Sprite();  
  33.             createSheet(sheet2, "There was a great story to tell, and it ended quickly.\n\nThe end."null);  
  34.             sheet3 = new Sprite();  
  35.             createSheet(sheet3, "你好,打印第三页!",null);  
  36.         }  
  37.           
  38.         private function createSheet(sheet:Sprite, str:String, imgValue:Object):void{  
  39.             sheet.graphics.beginFill(0xeeeeee);  
  40.             sheet.graphics.lineStyle(1,0x000000);  
  41.             sheet.graphics.drawRect(0,0,100,200);  
  42.             sheet.graphics.endFill();  
  43.               
  44.             var txt:TextField = new TextField();  
  45.             txt.height = 200;  
  46.             txt.width = 100;  
  47.             txt.wordWrap = true;  
  48.             txt.text = str;  
  49.             if(imgValue != null){  
  50.                 var img:Sprite = new Sprite();  
  51.                 img.graphics.beginFill(0x0066cc);  
  52.                 img.graphics.drawRect(imgValue.x, imgValue.y, imgValue.width, imgValue.height);  
  53.                 img.graphics.endFill();  
  54.                 sheet.addChild(img);  
  55.             }  
  56.             sheet.addChild(txt);  
  57.         }  
  58.           
  59.         private function printPages():void{  
  60.             var pj:PrintJob = new PrintJob();  
  61.             var pagesToPrint:uint = 0;  
  62.             if(pj.start()){  
  63.                 if(pj.orientation == PrintJobOrientation.LANDSCAPE){  
  64.                     throw new Error("Page is not set to an orientation of portrait.");  
  65.                 }  
  66.                   
  67.                 sheet1.height = pj.pageHeight;  
  68.                 sheet1.width = pj.pageWidth;  
  69.                 sheet2.height = pj.pageHeight;  
  70.                 sheet2.width = pj.pageWidth;  
  71.                 sheet3.height = pj.pageHeight;  
  72.                 sheet3.width = pj.pageWidth;  
  73.                 try{  
  74.                     pj.addPage(sheet1);  
  75.                     pagesToPrint++;  
  76.                 }catch(e:Error){  
  77.                     //响应错误  
  78.                 }  
  79.                 try{  
  80.                     pj.addPage(sheet2);  
  81.                     pagesToPrint++;  
  82.                 }catch(e:Error){  
  83.                     //响应错误  
  84.                 }  
  85.                 try{  
  86.                     pj.addPage(sheet3);  
  87.                     pagesToPrint++;  
  88.                 }catch(e:Error){  
  89.                     //响应错误  
  90.                 }  
  91.                   
  92.                   
  93.                 if(pagesToPrint>0){  
  94.                     pj.send();  
  95.                 }  
  96.             }  
  97.         }  
  98.   
  99.     }  
  100.       
  101. }  


多页打印效果图:


0 0
原创粉丝点击