Guest User

Untitled

a guest
Jan 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. package utils
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.DisplayObject;
  5. import flash.display.Loader;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.geom.Rectangle;
  9. import flash.net.URLRequest;
  10. import flash.printing.*;
  11.  
  12. import com.greensock.layout.*
  13.  
  14.  
  15. /**
  16. * @usage var page:A4Page = new A4Page(new Content());
  17. * page.print();
  18. * @author Dave Stewart
  19. */
  20. public class A4Page extends Sprite
  21. {
  22. // ---------------------------------------------------------------------------------------------------------------------
  23. // { region: Variables
  24.  
  25. // stage instances
  26. var page :Sprite;
  27. var area :AutoFitArea;
  28.  
  29. // properties
  30. var loader :Loader;
  31.  
  32.  
  33. // variables
  34.  
  35.  
  36. // ---------------------------------------------------------------------------------------------------------------------
  37. // { region: Instatiation
  38.  
  39. public function A4Page(content:DisplayObject = null)
  40. {
  41. // elements
  42. page = new Sprite();
  43. addChild(page);
  44.  
  45. area = new AutoFitArea(page, 10, 10, 822, 575);
  46.  
  47. // content
  48. if (content)
  49. {
  50. this.content = content;
  51. }
  52. }
  53.  
  54. // ---------------------------------------------------------------------------------------------------------------------
  55. // { region: Public methods
  56.  
  57. /**
  58. * Prints the content
  59. */
  60. public function print():void
  61. {
  62. var printJob :PrintJob = new PrintJob();
  63. var state :Boolean = printJob.start();
  64.  
  65. if(state)
  66. {
  67. trace
  68. (
  69. [
  70. printJob.paperWidth,
  71. printJob.paperHeight,
  72. printJob.pageWidth,
  73. printJob.pageHeight,
  74. printJob.orientation
  75. ]
  76. );
  77.  
  78. if (printJob.orientation == PrintJobOrientation.PORTRAIT)
  79. {
  80. page.rotation = -90;
  81. }
  82.  
  83. printJob.addPage(page);
  84. printJob.send();
  85.  
  86. }
  87.  
  88. }
  89.  
  90. // ---------------------------------------------------------------------------------------------------------------------
  91. // { region: Accessors
  92.  
  93. /**
  94. * Sets content and scales it to A4 page size
  95. */
  96. public function set content(content:DisplayObject):void
  97. {
  98. page.addChild(content);
  99. area.attach(content, ScaleMode.PROPORTIONAL_INSIDE, AlignMode.CENTER, AlignMode.CENTER, true);
  100. }
  101.  
  102. public function set url(url:String)
  103. {
  104. // loader
  105. loader = new Loader();
  106. loader.load(new URLRequest(url));
  107. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
  108. addChild(loader);
  109.  
  110. }
  111.  
  112. // ---------------------------------------------------------------------------------------------------------------------
  113. // { region: Handlers
  114.  
  115. protected function onLoadComplete(event:Event):void
  116. {
  117. // set bitmap and smoothing
  118. var bitmap :Bitmap = loader.content as Bitmap
  119. bitmap.smoothing = true;
  120.  
  121. // dispatch complete event
  122. this.content = loader;
  123.  
  124. // print
  125. print();
  126. }
  127.  
  128.  
  129.  
  130. // ---------------------------------------------------------------------------------------------------------------------
  131. // { region: Protected methods
  132.  
  133.  
  134.  
  135. // ---------------------------------------------------------------------------------------------------------------------
  136. // { region: Utilities
  137.  
  138.  
  139.  
  140. }
  141.  
  142. }
Add Comment
Please, Sign In to add comment