Printing in Flex 2
This morning I decided I needed to tackle printing for the project I'm working on. I more or less expected to spend a good part of the day figuring it out. Boy was I wrong! I had my project printing output
While it's entirely possible to get fancy (which I plan to do tomorrow) to start it's as simple as
import the class for flex printing:
Create a new PrintJob, then start the print job.
printJob.start();
then simply add items to the print job.
Each addObject is a new page in the printjob (so doing the above 10 times gets you 10 pieces of paper in the printer).
When you're all done, send your printjob to the printer (really you're sending it to the OS. From here the print dialogue will open in your OS
The object you pass the addObject() method can be a layout container (HBox, VBox, etc) or a single text object. My first run at printing, I wrapped my bits in a VBox, then passed the VBox to the addObject() method.
Once I got this solution soundly working I delved a bit deeper and found that a perhaps better solution is to create a printview component. Since often times (including my current project) your application looks a little different than what you'd like the printed output to look like. In my case I have some background colors and such that I don't want on the print out. So my printview will simply be a component devoid of any colors.
All in all printing in Flex 2 is incredibly easy, in fact IMO much easier than printing in CF/HTML

This is my print function
private function doPrint():void {
var printJob:FlexPrintJob = new FlexPrintJob();
var formPrintView:SubmissionReport = new SubmissionReport();
addChild(formPrintView);
var todayDate:Date = new Date();
formPrintView.detailUsername.text = _session.getSessionUsername();
formPrintView.detailDate.text = String(todayDate);
formPrintView.detailcontent.text = _session.returnSubmissionReportText()
printJob.start();
printJob.addObject(formPrintView);
printJob.send();
removeChild(formPrintView);
}
private function printText(header:String,body:String):void
{
var pj:PrintJob = new PrintJob();
var sheet:Sprite = new Sprite();
var ui:UIComponent = new UIComponent();
ui.visible=false;
if(pj.start())
{
//Fill in background with white
sheet.graphics.beginFill(0xFFFFFF);
sheet.graphics.drawRect(0, 0, pj.pageWidth, pj.pageHeight);
sheet.graphics.endFill();
//Create text field for header
var hdrFormat:TextFormat = new TextFormat("Arial",16,0x000000,true);
var headerTxt:TextField = new TextField();
headerTxt.defaultTextFormat=hdrFormat;
headerTxt.background = false;
headerTxt.border = false;
headerTxt.wordWrap = false;
headerTxt.width=pj.pageWidth;
headerTxt.y=25;
headerTxt.autoSize = TextFieldAutoSize.CENTER;
headerTxt.text = header;
sheet.addChild(headerTxt);
//Create text field for body
var bodyFormat:TextFormat = new TextFormat("Courier New",10,0x000000);
var bodyTxt:TextField = new TextField();
bodyTxt.defaultTextFormat=bodyFormat;
bodyTxt.background = false;
bodyTxt.border = false;
bodyTxt.wordWrap = true;
bodyTxt.x = 25;
bodyTxt.y = 50;
bodyTxt.multiline=true;
bodyTxt.width=pj.pageWidth-50;
bodyTxt.height=pj.pageHeight-100;
bodyTxt.text = body;
sheet.addChild(bodyTxt);
bodyTxt.scrollV = 1;
//Create text field for footer
var footerFormat:TextFormat = new TextFormat("Arial",10,0x000000);
var footerTxt1:TextField = new TextField();
footerTxt1.defaultTextFormat=footerFormat;
footerTxt1.background = false;
footerTxt1.border = false;
footerTxt1.wordWrap = false;
footerTxt1.width=pj.pageWidth;
footerTxt1.y=pj.pageHeight-40;
footerTxt1.autoSize = TextFieldAutoSize.CENTER;
sheet.addChild(footerTxt1);
var totalLines:int = bodyTxt.numLines; //Total lines in document
var linesPerPage:int = bodyTxt.bottomScrollV; //Number of lines per page
var curPage:int = 1; //Current page
var numPages:int = Math.ceil(totalLines / linesPerPage); //Total number of pages
//Append new lines to end of text
//This allows scrolling to work all the way to the end of the document
for(var i:int=0;i < linesPerPage;i++)
bodyTxt.appendText("\n");
//Add page to printJob, then scroll to next page
//Continue until last page
while(bodyTxt.bottomScrollV < totalLines)
{
footerTxt1.text = "Page "+curPage+" of "+numPages;
ui.addChild(sheet);
Application.application.addChild(ui);
pj.addPage(ui,null,new PrintJobOptions(true));
ui.removeChild(sheet); //Cleanup
Application.application.removeChild(ui); //Cleanup
bodyTxt.scrollV += linesPerPage; //Scroll text field to next page
curPage++;
if(curPage == 2)
sheet.removeChild(headerTxt); //Remove header after first page
}
//Add last page
footerTxt1.text = "Page "+curPage+" of "+numPages;
ui.addChild(sheet);
Application.application.addChild(ui);
pj.addPage(ui,null,new PrintJobOptions(true));
ui.removeChild(sheet); //Cleanup
Application.application.removeChild(ui); //Cleanup
pj.send(); //Send the print job
}
}