Raw text printing from Adobe AIR

Printing raw text in Adobe AIR is kinda pain, specially if the text is too large, or multipage!

After some research and gerring ideas, I derived my own version of Util class which manages basic functionality.

package
{
	import mx.utils.StringUtils;
	
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.printing.PrintJob;
	import flash.printing.PrintUIOptions;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class PrintUtils
	{
		public function PrintUtils()
		{}
		
		public static function printText(textString:String, printPageCount:Boolean = false):Boolean
		{
			var marginX:Number = 10;
			var marginY:Number = 10;
			var printJob:PrintJob = new PrintJob();
			var uiOpt:PrintUIOptions = new PrintUIOptions();
			
			if (!PrintJob.supportsPageSetupDialog) 
				return false;
			
			printJob.showPageSetupDialog(); 
			if( !printJob.start2(uiOpt, true) )
			{
				printJob.terminate();
				return false;
			}
			
			try
			{
				var fontSize:int = 10;
				var fontColor:uint = 0x000000;
				var format:TextFormat = new TextFormat( null, fontSize , fontColor );
				var printableArea:Rectangle = new Rectangle(0, 0, printJob.printableArea.width, printJob.printableArea.height);
				var lastChar:Number = 1;
				var pageCounter:int = 1;
				var pageCounterLine:int = printPageCount ? 1 : 0;
				while(lastChar > 0)
				{
					var remainingTextField:TextField = new TextField();
					remainingTextField.width = printableArea.width - (marginX*2);
					remainingTextField.height = printableArea.height - (marginY*2);
					
					remainingTextField.autoSize = TextFieldAutoSize.LEFT;
					remainingTextField.defaultTextFormat = format;
					remainingTextField.multiline = true;
					remainingTextField.wordWrap = true;
					
					remainingTextField.text = textString;
					
					// text strip out logic
					var lastLineTextString:String = StringUtil.trim(remainingTextField.getLineText(remainingTextField.bottomScrollV-pageCounterLine));
					if(lastLineTextString=="")
					{
						// find next printable line
						for(var j:int=1 ; j<(remainingTextField.bottomScrollV-pageCounterLine) ; j++)
						{
							lastLineTextString = StringUtil.trim(remainingTextField.getLineText(remainingTextField.bottomScrollV - pageCounterLine - j));
							if(lastLineTextString!="")
								break;
						}
					}
					
					lastChar = textString.indexOf(lastLineTextString) + lastLineTextString.length;
					var pageString:String = StringUtil.trim(textString.substr(0, lastChar));
					textString = StringUtil.trim(textString.substr(lastChar+1));
					
					if( StringUtils.isBlankOrNull(textString) )
						lastChar = 0;
					
					// print grabbed text on one page----------------------------------------------
					var printableSprite:Sprite = new Sprite();
					printableSprite.graphics.clear();
					//printableSprite.graphics.beginFill(0x00FF00);
					//printableSprite.graphics.drawRect(marginX, marginY, printableArea.width, printableArea.height);
					//printableSprite.graphics.endFill();
					
					var textFieldPrint:TextField = new TextField();
					textFieldPrint.width = printableArea.width - (marginX*2);
					textFieldPrint.height = printableArea.height - (marginY*2);
					textFieldPrint.x = marginX;
					textFieldPrint.y = marginY;
					
					textFieldPrint.autoSize = TextFieldAutoSize.LEFT;
					textFieldPrint.defaultTextFormat = format;
					textFieldPrint.multiline = true;
					textFieldPrint.wordWrap = true;
					
					textFieldPrint.text = pageString;
					
					printableSprite.addChild(textFieldPrint);
					
					// add pageCount
					if(printPageCount)
					{
						var pgCountTextFieldPrint:TextField = new TextField();
						
						pgCountTextFieldPrint.autoSize = TextFieldAutoSize.LEFT;
						pgCountTextFieldPrint.defaultTextFormat = format;
						pgCountTextFieldPrint.multiline = false;
						pgCountTextFieldPrint.wordWrap = true;
						pgCountTextFieldPrint.text = "Page " + pageCounter;
						
						pgCountTextFieldPrint.width = printableArea.width - (marginX*2);
						pgCountTextFieldPrint.x = marginX;
						pgCountTextFieldPrint.y = printableArea.height - pgCountTextFieldPrint.height - (marginY*2);
						
						printableSprite.addChild(pgCountTextFieldPrint);
						
						pageCounter++;
					}
					printJob.addPage(printableSprite, printableArea); 
				}
			}
			catch(error:Error)
			{
				return false;
			}
			printJob.send();
			return true;
		}
	}
}

This handles multi page text based on selected page size and margin. Also has minor feature of adding page number and so. This isn’t certainly rich utility, but also is enough to get started to add more features.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

%d bloggers like this: