Flash / Flex / ActionScript/Development/Print

Материал из Web эксперт
Перейти к: навигация, поиск

Adding Print Functionality to Applications

 
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.printing.PrintJob;
         public class Main extends Sprite {
     
             private var _printableContent:Sprite;
             private var _textField:TextField;
             private var _loader:URLLoader;
     
             public function Main() {
                 _loader = new URLLoader();
                 _loader.load(new URLRequest("http://www.wbex.ru/m.txt"));
                 _loader.addEventListener(Event.ruPLETE, completeHandler);
                 _textField = new TextField();
                 _textField.width = 400;
                 _textField.multiline = true;
                 _textField.wordWrap = true;
                 _textField.autoSize = TextFieldAutoSize.LEFT;
     
                 _printableContent = new Sprite();
                 addChild(_printableContent);
                 _printableContent.addChild(_textField);
     
             }
             private function completeHandler(event:Event):void {
                 _textField.text = _loader.data;
     
                 var printJob:PrintJob = new PrintJob();
                 if(printJob.start()) {
                     printJob.addPage(_printableContent);
                     printJob.send();
                 }
             }
         }
     }



Add pages to print job

 
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.printing.PrintJob;
     
         public class Main extends Sprite {
     
             private var _printableContent:Sprite = new Sprite();
             private var _textField:TextField = new TextField();
     
             private var _loader:URLLoader = new URLLoader();
     
             public function Main() {
                 _loader.load(new URLRequest("http://www.wbex.ru/m.txt"));
                 _loader.addEventListener(Event.ruPLETE, completeHandler);
     
                 _textField.width = 400;
                 _textField.multiline = true;
                 _textField.wordWrap = true;
                 _textField.autoSize = TextFieldAutoSize.LEFT;
     
                 addChild(_printableContent);
                 _printableContent.addChild(_textField);
             }
     
             private function completeHandler(event:Event):void {
     
                 _textField.text = _loader.data;
     
                 var printJob:PrintJob = new PrintJob();
                 if(printJob.start()) {
     
                     _textField.scaleY = printJob.pageHeight / _textField.height;
                     _textField.scaleX = _textField.scaleY;
                     printJob.addPage(_printableContent);
                     printJob.send();
     
                     _textField.scaleY = 1;
                     _textField.scaleX = 1;
                 }
             }
         }
     }



Print out a text file

 
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.printing.PrintJob;
    public class Main extends Sprite {
             private var _printableContent:Sprite = new Sprite();
             private var _textField:TextField = new TextField();
             private var _loader:URLLoader = new URLLoader();
     
             public function Printing() {
                 _loader.load(new URLRequest("http://www.wbex.ru/sum.txt"));
                 _loader.addEventListener(Event.ruPLETE, completeHandler);
                 _textField.width = 400;
                 _textField.multiline = true;
                 _textField.wordWrap = true;
                 _textField.autoSize = TextFieldAutoSize.LEFT;
     
                 addChild(_printableContent);
                 _printableContent.addChild(_textField);
     
             }
     
             private function completeHandler(event:Event):void {
                 _textField.text = _loader.data;
     
                 var printJob:PrintJob = new PrintJob();
                 if(printJob.start()) {
     
                     _textField.width = printJob.pageWidth;
                     printJob.addPage(_printableContent);
                     printJob.send();
     
                     _textField.width = 400;
                  }
             }
         }
     }



Print the text over many pages

 
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.printing.PrintJob;
         import flash.geom.Rectangle;
         public class Printing extends Sprite {
     
             private var _printableContent:Sprite = new Sprite();
             private var _textField:TextField = new TextField();
             private var _loader:URLLoader = new URLLoader();
     
             public function Printing() {
                 _loader.load(new URLRequest("http://www.wbex.ru/m.txt"));
                 _loader.addEventListener(Event.ruPLETE, completeHandler);
     
                 _textField.width = 400;
                 _textField.multiline = true;
                 _textField.wordWrap = true;
                 _textField.autoSize = TextFieldAutoSize.LEFT;
     
                 addChild(_printableContent);
                 _printableContent.addChild(_textField);
             }
     
             private function completeHandler(event:Event):void {
                 _textField.text = _loader.data;
                 var printJob:PrintJob = new PrintJob();
                 if(printJob.start()) {
                     var pages:Number = Math.ceil(_textField.height /  printJob.pageHeight);
     
                     _textField.width = printJob.pageWidth;
     
                     for(var i:Number = 0; i < pages; i++) {
                         printJob.addPage(_printableContent, new Rectangle(0, i *  printJob.pageHeight, printJob.pageWidth, printJob.pageHeight));
                     }
                     printJob.send();
                     _textField.width = 400;
                 }
             }
         }
     }