Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Job Not Doing The Job

Status
Not open for further replies.

theogary

Programmer
Mar 3, 2003
99
US
This print job function looks good in theory but is not printing a 2 page document correctly. It print 2 pages of 45 lines. Page 1 is printing lines 1 to 45. PAge 2 is printing line 24 to the end of the document. dont this the vPosition is working. Here is trhe code:

// 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;
}
 
Your approach is a good idea but the problem is your final print page is always start with somewhere in the penultimate page - because you cannot scroll beyond the length of the text. (I hope you get what I'm trying to say!)

If you want I can show you the code using another way: keep adding the words to the TextField and when the height exceeds the max printing height addPage() to the PrintJob, start again with the clean slate and start adding the words from that point on until the height hits the max point...

This works well, but it's slow - a lot of repeated calculation for Flash. Can't think of any other way at the moment though.

Kenneth Kawamoto
 
I wolud like to do what you suggested but I load the text field with the entire xml document initially. How do I get rid of the text that has printed alreadly or How do I load only 45 lines of text to text field at a time. I only know how to control the text by scrolling and setting theTextarea.vPosition property.
 
You can't scroll TextArea beyond the last line.

What I would do is use TextField (not TextArea), which can change the height according to the content ("autoSize") so that when the height exceeds certain amount you would know you have to addPage().

Kenneth Kawamoto
 
I there a way to insert lines into the text area. I would make a 75 line document into a 90 line document by adding 15 lines to get 2 45 line pages. I could have a function to create 45 line pages if I could add lines to the text area...
 
You can add the string "\n" - but your approach only works with your particular set-up, because you cannot assume how many lines are in the TextArea with different text, different platform, etc, etc.

Kenneth Kawamoto
 
That technique is proving unreliable. The textfield approach might work. Do you load everthing into the text field first and take snap shops of the 2 page document with the autosize property
 
Yes it works. But not like that - what I'd do is add word by word until the height hit the max print height, then addPage() to the PrintJob, start again with the next word and keep adding the word until the height hit the max... Repeat this until all the words are done, then send() the PrintJob.

Kenneth Kawamoto
 
I dont see a textfield component did you mean text input. if so I dont see autosize as property
 
I see that a text field can be created dynamically and is very similiar to a textarea field. But autosize seems to change the justification of the text field. How can(autosize) this control the amount of text displayed before addpage is called. do you have any code or link or tutorial to display this method
 
Code:
var veryLongText:String = "..."; // a very long text

var tf:TextField = this.createTextField("tf", 1, 0, 0, 500, 1);
with (tf) {
	wordWrap = true;
	multiline = true;
	autoSize = true;
	setNewTextFormat(new TextFormat("Verdana", 12));
}

var pj:PrintJob = new PrintJob();
if (pj.start()) {
	var wordArray:Array = veryLongText.split(" ");
	var currentStr:String = "";
	for (var i:Number = 0, len:Number = wordArray.length; i<len; i++) {
		var newStr:String = currentStr+wordArray[i]+" ";
		tf.text = newStr;
		if (tf._height>600) {
			tf.text = currentStr;
			pj.addPage(tf);
			currentStr = wordArray[i];
			tf.text = currentStr;
		} else {
			currentStr = newStr;
		}
	}
	pj.addPage(tf);
	pj.send();
	delete pj;
}

Kenneth Kawamoto
 
Thank you very much for time and your code. People like you keep the tek-tip forums a valuable resource.

There is no waaaay a newbie could come up with this logic.

Macromewdia should create a print component similar to this code. This is way too complicated for a print operation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top