The PrintText() Function
At last you come to the PrintText()function, which is where all this comes together.
If you want, go ahead and add the following snippet of code to the Actions associated with the first frame of the code layer:
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()) { counter = 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; }
First—and most importantly—the PrintText() function accepts one parameter: a TextArea object. If you’ll recall, when the event for the print button was defined, the function call was made this way:
- PrintText(mc2.text2);
This means that you want to send the parameter (or argument) mc2.text2 to the function.
First off, once you are in the function, you need to define a couple of variables that will help you later:
- visibleRows
- maxScroll
The visibleRows variable is set via a variable found in the ScrollView class, which is a class file found in /your_Flash_installation_directory/First Run/Classes/mx/core (assuming that you are using Flash 8).
This is a class that is called by the TextArea class, which for the curious is found in /your_Flash_installation_directory/First Run/Classes/mx/controls.
To get the value of maxScroll, simply add theTextArea’s maxVPosition to visibleRows. The maxVPosition property determines the vPosition (or vertical scroll position) of the text within a given TextArea.
var maxScroll:Number = theTextarea.maxVPosition + visibleRows;
After setting these variables, I turn off the vertical scroll bar for pretty printing.
theTextarea.vSB.visible = false;