Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
Like this article? We recommend

The PrintText() Function’s PrintJob

To borrow from the inimitable Frank Zappa, you have now arrived at "the crux of the biscuit." Creating the PrintJob object will give you control of how you finish this printout.

So first, create the PrintJob object. Pretty straightforward:

// instantiate the PrintJob
var the_pj:PrintJob = new PrintJob();

Next, you need to test the PrintJob. Do this with the_pj.start() method. It checks to make sure that the user has actually clicked the Print button in the system Print dialog box that pops up after clicking the print_btn in the application.

If all is a go, you need to create a counter variable initialized to zero, which will help you keep track of visible rows. Then we can proceed.

First, check the maxScroll variable to make sure that it is not isNaN:

if (! isNaN(maxScroll) ) {

This translates to "if maxScroll is not, not a number...," which translates into general human parlance as, "is maxScroll a number?" If it’s not, you need to go to the else part of the if statement because there are not multiple pages to print. In the else part, just call the addPage method and leave it at that.

} else {
   // if there is only one page... that is, maxScroll is NaN, just add the current page and move on
   the_pj.addPage(thetextarea, {xMin:-25, xMax:600, yMin:-30, yMax:800}, {printAsBitmap:false});
}

If, however, maxScroll is a number, it means you have more than just one page to print, so there is a bit more code. It means that although the counter, which started as zero, is less than maxScroll, continues to set theTextArea’s vPosition to the counter and incrementing the counter based on visibleRows (which translates to theTextArea’s page height, so to speak). Each time the counter is incremented by the visibleRows number, theTextArea scrolls down by that amount.

if (! isNaN(maxScroll) ) { 
     // while the counter is less than the maxScroll, keep adding pages to the print job
     while (counter < maxScroll) { 
         // scroll based on the counter position
         theTextarea.vPosition = counter;
         // increment the counter by the value of visibleRows (so page down)
         counter += visibleRows; 
         // add the page to the print job
         the_pj.addPage(theTextarea, {xMin:-25, xMax:600, yMin:-30, yMax:800}, {printAsBitmap:false});
     }
}

Once that is done, call the addPage on the_pj, and when it’s all said and done, and the program has broken out of the while loop, call the send() method, which sends the print job to the printer.

Finally, delete PrintJob Object, the_pj, because you don’t need it any more. All you need now is to grab the printout from the printer.

// send and delete the print job
the_pj.send();
delete the_pj;

I hope that this tutorial proved helpful to you. Now that you’ve gotten this down, you might try hiding offscreen TextArea offstage by either moving the TextArea itself or by moving its parent movieclip.

mc2.text2.move(1000, 80);

or...

mc2._x = 1000;

As I said, I am sure there are many ways in which it could be improved for real-world applications (and I hope I haven’t missed anything).

So good luck—and thanks for reading.

// set scaleMode to noScale so text fields will stay the same size no matter the size of the window
Stage.scaleMode = "noScale";

// import the textarea control
import mx.controls.TextArea;

// define styles... one for the screen and one for the print
var screenStyles = new TextField.StyleSheet();
     screenStyles.setStyle("html", {
         fontFamily: ’verdana,sans-serif’, 
                fontSize: ’12px’,
                color: ’#cc0000’}
     );

var printStyles = new TextField.StyleSheet();
     printStyles.setStyle("html", {     
         fontFamily: ’verdana,sans-serif’, 
                fontSize: ’9px’,
                color: ’#000000’}
     );

// Define on-screen textarea
this.createEmptyMovieClip("mc1", this.getNextHighestDepth() );

mc1.createClassObject(TextArea, "text1", mc1.getNextHighestDepth());
mc1.text1.move(10, 80);
mc1.text1.setSize(340, 110);
mc1.text1.html = true;
mc1.text1.wordWrap = true;
mc1.text1.styleSheet = screenStyles;
mc1.text1.antiAliasType = "advanced";
mc1.text1.text ="on-screen version<br /> <br />" + textValue;

// Define printable textarea
this.createEmptyMovieClip("mc2", this.getNextHighestDepth() );

mc2.createClassObject(TextArea, "text2", mc2.getNextHighestDepth());
// uncomment the following to move the printable textarea off the stage
//mc2._x = 1000;

mc2.text2.move(400, 80);
// a good size for printing out on American, standard letter-sized paper
mc2.text2.setSize(525, 650);
mc2.text2.html = true;
mc2.text2.wordWrap = true;
mc2.text2.styleSheet = printStyles;
mc2.text2.antiAliasType = "advanced";
mc2.text2.text = "printer version<br /> <br />" + textValue;

print_btn.onRelease = function() {
     // give the function the name of the textarea to print
     PrintText(mc2.text2);
}

function PrintText(theTextarea:Object) {
     // determine number of rows 
     var visibleRows:Number = theTextarea.viewableRows; 
     trace("visibleRows " + visibleRows);
     
     // to get the maxScroll, add the number of visible rows to the maxVPosition
     // this gives us a number sufficiently high enough to enable the while statement to get to the bottom of the textarea
     var maxScroll:Number = theTextarea.maxVPosition + visibleRows;
     trace("maxScroll " + maxScroll);
     trace("is maxScroll Not a number? " + isNaN(maxScroll) );
     
     // hide scrollbar for printout
     theTextarea.vSB.visible = false;
     
     // instantiate the PrintJob
     var the_pj:PrintJob = new PrintJob();
     
     // test the print job
     if (the_pj.start()) {
         var counter:Number = 0;
         // if maxScroll is a number, meaning that the textarea is in fact scrollable, proceed
         // otherwise,just add the single page to the print job and send it to the printer
         if (! isNaN(maxScroll) ) { 
              // while the counter is less than the maxScroll, keep adding pages to the print job
              while (counter < maxScroll) { 
                  // scroll based on the counter position
                  theTextarea.vPosition = counter;
                  // increment the counter by the value of visibleRows (so page down)
                  counter += visibleRows; 
                  // add the page to the print job
                  the_pj.addPage(theTextarea, {xMin:-25, xMax:600, yMin:-30, yMax:800}, {printAsBitmap:false});
              }
         } else {
              // if there is only one page... that is, maxScroll is NaN, just add the current page and move on
              the_pj.addPage(thetextarea, {xMin:-25, xMax:600, yMin:-30, yMax:800}, {printAsBitmap:false});
         }
              
         // send and delete the print job
         the_pj.send();
         delete the_pj;
     }
         
     
     // make scrollbar visible again 
     //thetextarea.vSB.visible = true;
}
  • + Share This
  • 🔖 Save To Your Account