AS3 using PrintJob to print a MovieClip
Asked Answered
G

3

5

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:

function printMovieClip(clip:MovieClip) {

var printJob:PrintJob = new PrintJob();
var numPages:int = 0;
var printY:int = 0;
var printHeight:Number;

if ( printJob.start() ) {

/* Resize movie clip to fit within page width */
if (clip.width > printJob.pageWidth) {
   clip.width = printJob.pageWidth;
   clip.scaleY = clip.scaleX;
}

numPages = Math.ceil(clip.height / printJob.pageHeight);

/* Add pages to print job */
for (var i:int = 0; i < numPages; i++) {
 printJob.addPage(clip, new Rectangle(0, printY, printJob.pageWidth, printJob.pageHeight));
 printY += printJob.pageHeight;
}

/* Send print job to printer */
printJob.send();

/* Delete job from memory */
printJob = null;

}

}

printMovieClip( testMC );

Unfortunately this is not working as expected i.e. printing the full width of the MovieClip and doing page breaks on the length.

Guardrail answered 14/9, 2009 at 15:26 Comment(0)
G
5

I forgot to scale the print area to match the movie clip being resized. See below for working solution:

function printMovieClip(clip:MovieClip) {

    var printJob:PrintJob = new PrintJob();
    var numPages:int = 0;
    var printArea:Rectangle;
    var printHeight:Number;
    var printY:int = 0;

    if ( printJob.start() ) {

        /* Resize movie clip to fit within page width */
        if (clip.width > printJob.pageWidth) {
            clip.width = printJob.pageWidth;
            clip.scaleY = clip.scaleX;
        }

        /* Store reference to print area in a new variable! Will save on scaling calculations later... */
        printArea = new Rectangle(0, 0, printJob.pageWidth/clip.scaleX, printJob.pageHeight/clip.scaleY);

        numPages = Math.ceil(clip.height / printJob.pageHeight);

        /* Add pages to print job */
        for (var i:int = 0; i < numPages; i++) {
            printJob.addPage(clip, printArea);
            printArea.y += printArea.height;
        }

        /* Send print job to printer */
        printJob.send();

        /* Delete job from memory */
        printJob = null;

    }

}

printMovieClip( testMC );
Guardrail answered 14/9, 2009 at 16:47 Comment(1)
If this works (I haven't tried it yet...), then thanks, because it's a very general and useful example of how to print a large MovieClip to span multiple pages in Flash AS3.Loathsome
P
2

Thank you for your Open-source spirit! Based on your great work, I implement it and make a little improvement to solve my practical MovieClip-printing problem. The major progress I have made is to work out a way of printing a MovieClip with multi-frames only by one printjob sending. Of course, I resolved the question of "printing the full width of the Movieclip". Because SWF stores content in the form of vectorgraph, what you need to do is make sure clip.height = printArea.height; clip.width = printArea.width;. It is a easy way:

 1//MC printing Function 
 2private function printMovieClip(clip:MovieClip):void
 3{
 4    var printJob:PrintJob=new PrintJob();
 5    var printArea:Rectangle;
 6    if (!printJob.start())
 7      return;
 8    //The page you choose to print ,"selectPages" is a mx:combox object i used to support printing one frame of MC
 9    var printPage:int=selectPages.selectedItem.data;
10    if (printPage == 0) //print all frames of the MovieClip
11      {
12        for (var i:int=1; i <= clip.totalFrames; i++)
13        {
14          clip.gotoAndStop(i);
15          /* Resize movie clip to fit within page width */
16          clip.width=printJob.pageWidth;
17          clip.scaleY=clip.scaleX;
18          /* Store reference to print area in a new variable! Will save on scaling */
19          printArea=new Rectangle(0, 0, printJob.pageWidth, printJob.pageHeight);
20          //numPages=Math.ceil(clip.height / printJob.pageHeight);
21                  /* Add pages to print job */
22          printJob.addPage(clip, printArea);
23        }
24      }
25    else //print the selected frame
26    {
         //goto the selected frame firstly
27        clip.gotoAndStop(printPage);
28        /* Resize movie clip to fit within page width */
29        clip.width=printJob.pageWidth;
30        clip.scaleY=clip.scaleX;
31        printArea=new Rectangle(0, 0, printJob.pageWidth, printJob.pageHeight);
32         /* Add pages to print job */
33        printJob.addPage(clip, printArea);
34        }
35
36        /* Send print job to printer */
37        printJob.send(); 
38         /* Delete job from memory */
39        printJob=null;
40
41    }

If you want more information, you can take a look at my clip image (and given you understand a little Chinese): it's all in my blog. There are also MovieClip thumbnails (still Chinese).

Physicalism answered 17/11, 2009 at 15:12 Comment(1)
I'd not even considered multiple frames, good work :) though as a suggestion, what about removing the reference to the mx:combobox from the function and making the "printPage" variable an optional argument? Would make it a wee bit more flexible.Guardrail
C
0

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;
            }

        }
Chastity answered 16/6, 2012 at 10:30 Comment(1)
I'm gonna have to try this, sounds ace. Thanks!Guardrail

© 2022 - 2024 — McMap. All rights reserved.