AS3 using PrintJob to print a MovieClip

前端 未结 3 1121
盖世英雄少女心
盖世英雄少女心 2020-12-30 15:40

I am currently trying to create a function which will allow me to pass in a MovieClip and print it.

Here is the simplified version of the function:

3条回答
  •  有刺的猬
    2020-12-30 16:15

    I added a small fix that resets the MovieClip's dimensions after the printjob is done. The problem was that when you print something that is larger then your page, the code would also scale the movieclip on stage. So i Fixed that... nothing special but might be useful for other people :)

    this code also fixes the fact that your transparent PNG's will ALSO be transparent on your print

    protected function printMovieClip(clip:MovieClip):void {
    
                var printJob:PrintJob = new PrintJob();
                var printJobOptions:PrintJobOptions = new PrintJobOptions();
                var numPages:int = 0;
                var printArea:Rectangle;
                var printHeight:Number;
                var printY:int = 0;
                var originalWidth:Number;
                var originalHeight:Number;
    
                if ( printJob.start() ) {
    
                    originalWidth = clip.width;
                    originalHeight = clip.height;
    
                    if (clip.width > printJob.pageWidth) {
                        clip.width = printJob.pageWidth;
                        clip.scaleY = clip.scaleX;
                    }
    
                    printArea = new Rectangle(0, 0, printJob.pageWidth/clip.scaleX, printJob.pageHeight/clip.scaleY);
    
                    numPages = Math.ceil(clip.height / printJob.pageHeight);
    
                    for (var i:int = 0; i < numPages; i++) 
                    {
                        printJobOptions.printAsBitmap = true;
                        printJob.addPage(clip, printArea, printJobOptions);
                        printArea.y += printArea.height;
                    }
    
                    /* Send print job to printer */
                    printJob.send();
    
                    /* Delete job from memory */
                    printJob = null;
    
                    /* reset the clips width and height on stage so it is back at its original size*/
                    clip.width = originalWidth;
                    clip.height = originalHeight;
                }
    
            }
    

提交回复
热议问题