Flash ActionScript 2 PrintJob function example

This ActionScript 2 code demonstrates how to use the PrintJob() function to generate a print job from JPG images in a subfolder.

In summary, what occurs is that all the images are imported/created as child movie clips of the parent movie clip, printZone, and then all the child movie clips are passed to the PrintJob object, pj.

The biggest problem with this code is that for some reason the print dialog box does not give you the option to print selected pages.

var printPath:String = "pages/"; // The is relative path to the subfolder containing the images.
var printLabel:String = "page-"; // For some reason I decided to use "page-" as the prefix for all the image files.
var printType:String = ".jpg"; // This is the file type for the images. You can use SWF files as well as PNGs instead, if you like.
var numPages:Number = 32; // The number of pages is hard coded but, given a chance to do it again, I'd probably go with an XML file definition.
var printXMin:Number = 0; // This variable and the next define the position on the page for the image. In this case, the upper left-hand corner is set to zero.
var printYMin:Number = 0;
var printXMax:Number = 1275; // This variable and the next define the number of pixels to force into the page from the source image.
var printYMax:Number = 1650; // And I set these variable to match the actual sizes of the JPG images so it would print the entire JPG image.
var printXScale:Number = 50; // An actual 8 1/2 X 11 sheet of paper is about 637.5 X 825 screen pixels and since the images are so much larger
var printYScale:Number = 50; // I had to scale them down 50% to fit on the page.

printCatalog = function() { // This function is called when some print button movie clip is released.
  loadingIndicator._visible = true; // The loadingIndicator movie clip nests a progressBar movie clip that is used to feedback progress for processes in general.
  var printLoader:MovieClipLoader = new MovieClipLoader(); // This is the MovieClipLoader object that manages to import the JPGs to the printZone movie clip.
  var printLoaderListener:Object = new Object(); // This object hosts the functions that are called for the MovieClipLoader object.
  var pageCount:Number = 1; // This variable keeps track of what page is being processed. 


  printLoaderListener.onLoadProgress = function (target_mc:MovieClip, bytesLoaded:Number, bytesTotal:Number) {// This function updates the loadingIndicator to show progress of loading the individual pages.
    loadingIndicator.progressBar._width = Math.round(bytesLoaded/bytesTotal*100);

  printLoaderListener.onLoadInit = function (mc:MovieClip) {//This function is called when the image is done loading.
    pageCount++; //When the image is loaded then the pageCounter is incremented. This could probably be put after the if-statement and the (numPages+1) term in the if-statement could be simply numPages.
    if (pageCount==(numPages+1)) {
      printDocument(); // If this is the last page then go ahead and print the document.
    } else {
      printNextPage(); // Only when the image is loaded do we proceed to load the next page image.    }

  printDocument = function () {//This function simply takes the entire array of movie clip children from printZone and adds them as pages to the PrintJob() object, pj.
    var pj = new PrintJob();
    if(pj.start()) {
      var i = new Number();
      for (i=1;i<=numPages;i++) {
        printZone["page"+i]._xscale = printXScale;
        printZone["page"+i]._yscale = printYScale;
        pj.addPage(eval("printZone.page"+i), {xMin:printXMin,xMax:printXMax,yMin:printYMin,yMax:printYMax},{printAsBitmap:true}, 1);
        loadingIndicator.progressBar._width = Math.round(i/numPages*100);//Here the progress of the pages adds is reflected in the loadingIndicator movie clip.
      loadingIndicator._visible = false;
      delete pj;

  printNextPage = function () { //This function parses the file name of the page and then
    var pageString = new String();
    if (pageCount<10) {// For whatever reason, I formatted the files to be "page-001.jpg, page-002.jpg, ... , page-010.jpg, etc,..." This if-then statement formats the page element to match.
      pageString = "00"+pageCount.toString();
    } else {
      pageString = "0"+pageCount.toString();
    var fileName:String = printPath+printLabel+pageString+printType;
    printZone.createEmptyMovieClip("page"+pageCount, pageCount);
    printLoader.loadClip(fileName, eval("printZone.page"+pageCount));// Here the image is called to load.

  printNextPage(); // So here's where we start by loading the first page.

This swath of code is fairly down-and-dirty. It could probably stand to have some error handlers. Also, a lot of this information could be offloaded to an XML file and make it more flexible.


Posted: July 14th, 2009 | Author: | Filed under: Code Reference | Tags: , | 1 Comment »

One Comment on “Flash ActionScript 2 PrintJob function example”

  1. 1 Charles David Purses said at 11:37 am on April 7th, 2010:

    I do not believe I’ve seen this said in such a way before. You really have clarified this for me. Thanks!

Leave a Reply