▼ActionScript 3.0メモ▼
印刷の実行


印刷の実行を行うFlashを作成する。


素材の準備

piyo.swf



ソースコード
PrintJobEx.as
package {
    import flash.display.*;
    import flash.events.*;
    import flash.printing.*;
       
    //印刷の実行
    public class PrintJobEx extends Sprite {
        [Embed(source='piyo.swf')]
        private var Piyo:Class;

        private var child:Sprite;//子スプライト
           
        //コンストラクタ
        public function PrintJobEx() {
            //子スプライト
            child=new Sprite();
            addChild(child);

            //Flash(SWF)の追加
            var piyo:Sprite=new Piyo();
            child.addChild(piyo);
            piyo.x=120;
            piyo.y=120;

            //イベントリスナーの指定
            child.addEventListener(MouseEvent.CLICK,clickHandler);
        }

        //マウスクリックイベントの処理
        private function clickHandler(evt:MouseEvent):void {
            var pj:PrintJob=new PrintJob();
            var width:uint;
            var height:uint;
            if (pj.start()) {                
                //ランドスケープ
                if(pj.orientation==PrintJobOrientation.LANDSCAPE) {
                    throw new Error();
                }
                
                try {
                    //印刷サイズの指定
                    width =child.width;
                    height=child.height;
                    child.width =pj.pageWidth;
                    child.height=pj.pageHeight;

                    //印刷の実行
                    pj.addPage(child);
                    pj.send();
                    
                    //サイズを戻す
                    child.width =width;
                    child.height=height;
                } catch(e:Error) {
                }
            }
        }    
    }
}



−戻る−