Dear all,
I would want to print out all the data inside my Datagrid which were populated in flash AS3. Taking into consideration that my datagrid has horizontalScrollPolicy and verticalScrollPolicy activated, how do I print the whole information?
This is the closest sample code I’m looking for, but I had alot of errors when I tried to use it, maybe because it is in AS2 & i convented it wrongly…
I got it to this far but I’m having some problems. I hope someone could help me out on this :bu:
The problem is it prints nothing in the 1st page and some weird layout in the 2nd page.
Can anyone take a look at the code and lend me a hand please? Please download the My convented AS3 code Link.
Sample AS2 code Link: http://www.mediafire.com/?z8z4x764n308kdz
My convented AS3 code Link: http://www.mediafire.com/?dcrck7zsn49gk08
import fl.data.DataProvider;
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.ScrollPolicy;
var dp:DataProvider = new DataProvider();
//create arbitray data
var SampleData = new Array();
for (var i = 0; i < 50; i ++)
{
dp.addItem({no:i, Winner_Name:"Name_"+i, Prize_Title:"Title_"+i, Prize_Name:"PrizeName_"+i});
SampleData.push(dp);
}
//trace(SampleData);
myDataGrid.columns= ["no","Winner_Name","Prize_Title","Prize_Name"];
myDataGrid.getColumnAt(0).width = 50;
myDataGrid.getColumnAt(1).width = 160;
myDataGrid.getColumnAt(2).width = 170;
myDataGrid.getColumnAt(3).width = 170;
myDataGrid.dataProvider = dp;
myDataGrid.horizontalScrollPolicy = ScrollPolicy.ON;
myDataGrid.setSize(463, 178);
clickme.addEventListener(MouseEvent.CLICK, clickmeFn)
function clickmeFn(e:MouseEvent){
trace("clickmeFn")
doPrint();
}
function doPrint()
{
var pagesToPrint:Number = 0;
var _objPrintJob:PrintJob = new PrintJob();
// display print dialog box
var _printOK:Boolean = _objPrintJob.start();
//abort if cancelled;
if (!_printOK)
{
//delete _objPrintJob;
_objPrintJob = null;
return;
}
//START ADDING PAGES FOR THE DATAGRID
//create new movieclip for the datagrid data
var printOutput:MovieClip = createDgPrintClip(myDataGrid,_objPrintJob);
//define the height of the page snapshots depending on how many rows fit
//into the pageheight returned by the printer
var _pageHeight:Number = Math.floor(_objPrintJob.pageHeight/printOutput.rowHeight)*printOutput.rowHeight;
var _yMin:Number = 0;
var xMargin:Number = -40;
var rightMargin = 30;
while(_yMin < printOutput.height)
{
var _yMax:Number = _yMin + _pageHeight;
if (_objPrintJob.addPage(printOutput, new Rectangle(xMargin, _objPrintJob.pageWidth + xMargin - rightMargin, _yMin, _yMax)))
{
pagesToPrint++;
}
_yMin = _yMax;
trace(pagesToPrint);
}
//END ADDING PAGES FOR THE DATAGRID
// send pages from the spooler to the printer
if (pagesToPrint > 0)
{
_objPrintJob.send();
}
// clean up
//delete _objPrintJob;
_objPrintJob = null;
//printOutput.removeMovieClip();
removeChild(printOutput);
}
function createDgPrintClip(targetDG:DataGrid,printJob:PrintJob):MovieClip
{
var headerFormat:TextFormat = new TextFormat()
headerFormat.size = 12;
headerFormat.font = "Arial";
headerFormat.bold = true;
headerFormat.leftMargin = 4;
var txtFormat:TextFormat = new TextFormat()
txtFormat.size = 12;
txtFormat.font = "Arial";
txtFormat.leftMargin = 4;
var _rowHeight:Number = 18;
//calculate rows per page taking off 4 rows for margin
var _rowsPerPage:Number = Math.floor(printJob.pageHeight/_rowHeight) - 4;
//note: the static value 100000 is used for the mc depth as using
//this.getNextHighestDepth() will result in the mc not being removed
//using removeMovieClip()
//var printClip:MovieClip = createEmptyMovieClip("print_mc", 100000);
var printClip:MovieClip = new MovieClip();
printClip.graphics.drawRect(0, 0, 100, 80);
printClip.x = 0;
printClip.y = 0;
addChild(printClip);
this.setChildIndex ( printClip , this.numChildren - 1 );
printClip.visible = false;
//get colums
//var columns:Array = targetDG.columnNames;
var columns:Array = ["Flash", "ActionScript", "Republic of Code"];
//trace(columns);
//Start printing headers
var xPos:Number = 0;
var tbWidth:Number = 0;
//leave 2 rows margin at top
var rowY:Number = _rowHeight * 2;
for (var i = 0; i < columns.length; i++)
{
//define xPos by adding the tbWidth of the last loop
xPos = xPos + tbWidth;
var column:DataGridColumn = targetDG.getColumnAt(i);
//get width of this column
tbWidth = column.width;
//add textField ActionScript 2.0
// printClip.createTextField("header_" + i , printClip.getNextHighestDepth(), xPos, rowY,tbWidth, _rowHeight);
//
// var thisTb:TextField = printClip["header_" + i];
// //thisTb.setNewTextFormat(headerFormat);
// thisTb.defaultTextFormat = headerFormat;
// thisTb.border = true;
// thisTb.borderColor = 0xCCCCCC;
// thisTb.background = true;
// thisTb.backgroundColor = 0xD5EAFF;
// thisTb.text = column.headerText;
// ActionScript 3.0
var thisTb:TextField = new TextField();
thisTb.x = xPos;
thisTb.y = rowY;
thisTb.width = tbWidth;
thisTb.height = _rowHeight;
thisTb.border = true;
thisTb.borderColor = 0xCCCCCC;
thisTb.background = true;
thisTb.backgroundColor = 0xD5EAFF;
thisTb.text = column.headerText;
printClip.addChild(thisTb)
}
//End printing headers
//Start printing rows
//start row counter taking in count the header row
var pageRows:Number = 1;
var pages:Number = 1;
var dataArray:Object = targetDG.dataProvider;
//add data rows
for (var j = 0; j < dataArray.length; j++)
{
//insert margins if rows per page are full
if (pageRows == _rowsPerPage)
{
rowY += _rowHeight * 5;
pageRows = 1;
pages++;
//add headers
xPos = 0;
tbWidth = 0;
for ( i = 0; i < columns.length; i++)
{
//define xPos by adding the tbWidth of the last loop
xPos = xPos + tbWidth;
var column:DataGridColumn = targetDG.getColumnAt(i);
//get width of this column
tbWidth = column.width;
//add textField ActionScript 2.0
//printClip.createTextField("header_" + pages +"_" + i , printClip.getNextHighestDepth(), xPos, rowY,tbWidth, _rowHeight);
// var thisTb:TextField = printClip["header_" + pages +"_" + i];
// //thisTb.setNewTextFormat(headerFormat);
// thisTb.defaultTextFormat = headerFormat;
// thisTb.border = true;
// thisTb.borderColor = 0xCCCCCC;
// thisTb.background = true;
// thisTb.backgroundColor = 0xD5EAFF;
// thisTb.text = column.headerText;
// ActionScript 3.0
var thisTb:TextField = new TextField();
thisTb.x = xPos;
thisTb.y = rowY;
thisTb.width = tbWidth;
thisTb.height = _rowHeight;
thisTb.border = true;
thisTb.borderColor = 0xCCCCCC;
thisTb.background = true;
thisTb.backgroundColor = 0xD5EAFF;
thisTb.text = column.headerText;
printClip.addChild(thisTb)
}
}
rowY += _rowHeight;
pageRows++
//add TextFields for each row
xPos = 0;
tbWidth = 0;
var rowData:Object = dataArray.getItemAt(j);
//trace("rowData="+rowData);
for (var i = 0; i < columns.length; i++)
{
//define xPos by adding the tbWidth of the last loop
xPos = xPos + tbWidth;
var column:DataGridColumn = targetDG.getColumnAt(i);
//get width of this column
tbWidth = column.width;
//add textField ActionScript 2.0
//printClip.createTextField("field_"+ j + "_" + i , printClip.getNextHighestDepth(), xPos, rowY,tbWidth, _rowHeight);
// var thisTb:TextField = printClip["field_"+ j + "_" + i];
// //thisTb.setNewTextFormat(txtFormat);
// thisTb.defaultTextFormat = txtFormat;
// thisTb.border = true;
// thisTb.borderColor = 0xCCCCCC;
// //as columns was defined as the array of field identifiers (= columnNames)
// //for the datagrid we can use the array items to identify the properties
// //in the dataProvider
// thisTb.text = rowData[columns*];
// ActionScript 3.0
var thisTb:TextField = new TextField();
thisTb.x = xPos;
thisTb.y = rowY;
thisTb.width = tbWidth;
thisTb.height = _rowHeight;
thisTb.border = true;
thisTb.borderColor = 0xCCCCCC;
thisTb.text = "rowData[columns*]";
trace(rowData[columns*]);
//trace("rowData="+rowData[columns[1]]);
printClip.addChild(thisTb)
}
}
//End printing rows
//store rowHeight with printclip to be retrieved later
printClip.rowHeight = _rowHeight;
return printClip;
}
EDIT:
My convented AS3 version2 code Link: http://www.mediafire.com/?g8y0cn7451klfyd
Now in version 2, I deleted
if (_objPrintJob.addPage(printOutput, new Rectangle(xMargin, _objPrintJob.pageWidth + xMargin - rightMargin, _yMin, _yMax)))
and replaced it with
if (_objPrintJob.addPage(printOutput))
it prints 2 pages of the same content instead… Need help…
-Zainuu