·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3打印功能的源代码

时间:2014-08-11 08:45酷播
[AS3]as3打印功能的源代码

 as3打印功能单页

  1. package{   
  2.  
  3.        
  4.  
  5.     import flash.display.Sprite;   
  6.  
  7.     import flash.printing.PrintJob;   
  8.  
  9.     import flash.printing.PrintJobOptions;   
  10.  
  11.     import flash.printing.PrintJobOrientation;   
  12.  
  13.     import flash.geom.Rectangle;   
  14.  
  15.     import flash.events.MouseEvent;   
  16.  
  17.        
  18.  
  19.     public class BasicPrintExample extends Sprite{   
  20.  
  21.            
  22.  
  23.         private var myPrintJob:PrintJob = new PrintJob();   
  24.  
  25.         private var mySprite:Sprite = new Sprite();   
  26.  
  27.         private var options:PrintJobOptions = new PrintJobOptions();   
  28.  
  29.         private var rect1:Rectangle = new Rectangle(0,0,400,200);   
  30.  
  31.            
  32.  
  33.         public function BasicPrintExample(){   
  34.  
  35.                
  36.  
  37.             addChild(mySprite);   
  38.  
  39.             mySprite.addChild(mc);   
  40.  
  41.                
  42.  
  43.             btn.addEventListener(MouseEvent.CLICK, btnClick);   
  44.  
  45.         }   
  46.  
  47.            
  48.  
  49.         private function btnClick(e){   
  50.  
  51.             printJob();   
  52.  
  53.         }   
  54.         private function printJob(){   
  55.             options.printAsBitmap = true;   
  56.             myPrintJob.start();   
  57.             myPrintJob.addPage(mySprite,rect1,options);   
  58.             myPrintJob.send();   
  59.         }   
  60.     }   
  61.  
  62. }   

as3打印功能多页打印

  1. package  {   
  2.  
  3.     //多页打印类   
  4.  
  5.     import flash.display.MovieClip;   
  6.  
  7.     import flash.printing.PrintJob;   
  8.  
  9.     import flash.printing.PrintJobOrientation;   
  10.  
  11.     import flash.display.Stage;   
  12.  
  13.     import flash.display.Sprite;   
  14.  
  15.     import flash.text.TextField;   
  16.  
  17.     import flash.geom.Rectangle;   
  18.  
  19.     import flash.events.MouseEvent;   
  20.  
  21.        
  22.  
  23.     public class PrintMultiplePages extends MovieClip {   
  24.  
  25.    
  26.  
  27.         private var sheet1:Sprite;   
  28.  
  29.         private var sheet2:Sprite;   
  30.  
  31.         private var sheet3:Sprite;   
  32.  
  33.    
  34.  
  35.         public function PrintMultiplePages() {   
  36.  
  37.             // constructor code   
  38.  
  39.             init();   
  40.  
  41.                
  42.  
  43.             btn.addEventListener(MouseEvent.CLICK, btnClick);   
  44.  
  45.         }   
  46.  
  47.            
  48.  
  49.         private function btnClick(e):void{   
  50.  
  51.             printPages();//打印   
  52.  
  53.         }   
  54.  
  55.            
  56.  
  57.         private function init():void{   
  58.  
  59.             sheet1 = new Sprite();   
  60.  
  61.             createSheet(sheet1, "Once upon a time...",{x:10, y:50, width:80, height:130});   
  62.  
  63.             sheet2 = new Sprite();   
  64.  
  65.             createSheet(sheet2, "There was a great story to tell, and it ended quickly.\n\nThe end.", null);   
  66.  
  67.             sheet3 = new Sprite();   
  68.  
  69.             createSheet(sheet3, "你好,打印第三页!",null);   
  70.  
  71.         }   
  72.  
  73.            
  74.  
  75.         private function createSheet(sheet:Sprite, str:String, imgValue:Object):void{   
  76.  
  77.             sheet.graphics.beginFill(0xeeeeee);   
  78.  
  79.             sheet.graphics.lineStyle(1,0x000000);   
  80.  
  81.             sheet.graphics.drawRect(0,0,100,200);   
  82.  
  83.             sheet.graphics.endFill();   
  84.  
  85.                
  86.  
  87.             var txt:TextField = new TextField();   
  88.  
  89.             txt.height = 200;   
  90.  
  91.             txt.width = 100;   
  92.  
  93.             txt.wordWrap = true;   
  94.  
  95.             txt.text = str;   
  96.  
  97.             if(imgValue != null){   
  98.  
  99.                 var img:Sprite = new Sprite();   
  100.  
  101.                 img.graphics.beginFill(0x0066cc);   
  102.  
  103.                 img.graphics.drawRect(imgValue.x, imgValue.y, imgValue.width, imgValue.height);   
  104.  
  105.                 img.graphics.endFill();   
  106.  
  107.                 sheet.addChild(img);   
  108.  
  109.             }   
  110.  
  111.             sheet.addChild(txt);   
  112.  
  113.         }   
  114.  
  115.            
  116.  
  117.         private function printPages():void{   
  118.  
  119.             var pj:PrintJob = new PrintJob();   
  120.  
  121.             var pagesToPrint:uint = 0;   
  122.  
  123.             if(pj.start()){   
  124.  
  125.                 if(pj.orientation == PrintJobOrientation.LANDSCAPE){   
  126.  
  127.                     throw new Error("Page is not set to an orientation of portrait.");   
  128.  
  129.                 }   
  130.  
  131.                    
  132.  
  133.                 sheet1.height = pj.pageHeight;   
  134.  
  135.                 sheet1.width = pj.pageWidth;   
  136.  
  137.                 sheet2.height = pj.pageHeight;   
  138.  
  139.                 sheet2.width = pj.pageWidth;   
  140.  
  141.                 sheet3.height = pj.pageHeight;   
  142.  
  143.                 sheet3.width = pj.pageWidth;   
  144.  
  145.                 try{   
  146.  
  147.                     pj.addPage(sheet1);   
  148.  
  149.                     pagesToPrint++;   
  150.  
  151.                 }catch(e:Error){   
  152.  
  153.                     //响应错误   
  154.  
  155.                 }   
  156.  
  157.                 try{   
  158.  
  159.                     pj.addPage(sheet2);   
  160.  
  161.                     pagesToPrint++;   
  162.  
  163.                 }catch(e:Error){   
  164.  
  165.                     //响应错误   
  166.  
  167.                 }   
  168.  
  169.                 try{   
  170.  
  171.                     pj.addPage(sheet3);   
  172.  
  173.                     pagesToPrint++;   
  174.  
  175.                 }catch(e:Error){   
  176.  
  177.                     //响应错误   
  178.  
  179.                 }   
  180.  
  181.                    
  182.  
  183.                    
  184.  
  185.                 if(pagesToPrint>0){   
  186.  
  187.                     pj.send();   
  188.  
  189.                 }   
  190.  
  191.             }   
  192.  
  193.         }   
  194.  
  195.     }    
  196.  
  197. }   

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器