[ActionScript 3.0] PrintJob打印功能

 1 package

 2 {

 3     import flash.display.Bitmap;

 4     import flash.display.Sprite;

 5     import flash.events.MouseEvent;

 6     import flash.geom.Rectangle;

 7     import flash.printing.PrintJob;

 8     import flash.printing.PrintJobOptions;

 9     import flash.text.TextField;

10     import flash.text.TextFormat;

11     import flash.utils.getDefinitionByName;

12     

13     /**

14      * @author Frost.Yen

15      * @E-mail [email protected]

16      * @create 2015-7-27 下午1:58:56

17      *

18      */

19     [SWF(width="800",height="600")]

20     public class PrintTest extends Sprite

21     {

22         private var _printJob:PrintJob = new PrintJob();

23         private var _printOptions:PrintJobOptions = new PrintJobOptions();

24         private var _sp:Sprite = new Sprite();

25         private var _text:TextField = new TextField();

26         [Embed(source="test.png")] 

27         private var _png:Class; 

28         public function PrintTest()

29         {

30             initViews();

31         }

32         private function initViews():void

33         {

34             var string:String = "PrintTest__png";//所在类名+"_"+embed的类名 

35             var c:Class = getDefinitionByName(string) as  Class; 

36             var b:Bitmap = new c(); 

37             _text.selectable = false;

38             _text.border = true;

39             _text.autoSize = "left";

40             _text.text = "Click me to print";

41             _text.setTextFormat(new TextFormat("Arial",16,0));

42             _text.x = _text.y = 10;

43             _sp.x = 150;

44             _sp.y = 10;

45             this.addChild(_text);

46             this.addChild(_sp);

47             _sp.addChild(b);

48             initEventListeners();

49         }

50         private function initEventListeners():void

51         {

52             _text.addEventListener(MouseEvent.CLICK,onPrint);

53         }

54         private function onPrint(e:MouseEvent):void

55         {

56             _printOptions.printAsBitmap = true;

57             if(_printJob.start()) { 

58                 try {

59                     _printJob.addPage(_sp,new Rectangle(0,0,_sp.width,_sp.height),_printOptions);

60                 } catch(e:Error) {

61                     trace(e.toString());

62                 }

63                 _printJob.send(); 

64             }

65         }

66     }

67 }

 

你可能感兴趣的:(actionscript)