打印示例:页面设置和打印选项

Adobe AIR 2 和更高版本

在以下示例中,对下列 PrintJob 设置进行了初始化:打印份数、页面大小(法律专用纸)和页面方向(横向)。此示例强制首先显示“页面设置”对话框,然后显示“打印”对话框以启动打印作业。

package 
{ 
    import flash.printing.PrintJob; 
    import flash.printing.PrintJobOrientation; 
    import flash.printing.PaperSize; 
    import flash.printing.PrintUIOptions; 
    import flash.display.Sprite; 
    import flash.text.TextField; 
    import flash.display.Stage; 
    import flash.geom.Rectangle; 
     
    public class PrintAdvancedExample extends Sprite 
    { 
        private var bg:Sprite = new Sprite(); 
        private var txt:TextField = new TextField(); 
        private var pj:PrintJob = new PrintJob(); 
        private var uiOpt:PrintUIOptions = new PrintUIOptions(); 
 
        public function PrintAdvancedExample():void 
        { 
            initPrintJob(); 
            initContent(); 
            draw(); 
            printPage(); 
        } 
         
        private function printPage():void 
        { 
            //test for dialog support as a static property of PrintJob class 
            if (PrintJob.supportsPageSetupDialog) 
            { 
                pj.showPageSetupDialog(); 
            } 
            if (pj.start2(uiOpt, true)) 
            { 
                 try 
                { 
                    pj.addPage(this, new Rectangle(0, 0, 100, 100)); 
                } 
                catch (error:Error) 
                { 
                    // Do nothing. 
                } 
                pj.send(); 
            } 
            else 
            { 
                txt.text = "Print job terminated"; 
                pj.terminate(); 
            } 
        } 
         
        private function initContent():void 
        { 
            bg.graphics.beginFill(0x00FF00); 
            bg.graphics.drawRect(0, 0, 100, 200); 
            bg.graphics.endFill(); 
             
            txt.border = true; 
            txt.text = "Hello World"; 
        } 
         
        private function initPrintJob():void 
        { 
            pj.selectPaperSize(PaperSize.LEGAL); 
            pj.orientation = PrintJobOrientation.LANDSCAPE; 
            pj.copies = 2; 
            pj.jobName = "Flash test print"; 
        } 
         
        private function draw():void 
        { 
            addChild(bg); 
            addChild(txt); 
            txt.x = 50; 
            txt.y = 50; 
        } 
    } 
}