After trying a lot of examples form adobe and others that did not work at all for me, I figured out that I had to put the datagrid in a vBox where I could put a blank header above the datagrid. As all of the examples showed, this was best done with a vBox component.
Below is a working example of what I came up with, which is a bit different than the examples I found. In the vBox component you can do a lot of things to enhance the print job. In my case I added a title and date.
Hope this helps someone out there,
John
I am tusing a generic function to print any AdvancedDataGrid I might have in my application...
public function printAdvancedDataGridContents(advDG:AdvancedDataGrid, xmlListCollection:XMLListCollection, headerText:String):void
{
const printJob:FlexPrintJob = new FlexPrintJob();
if ( printJob.start() ) {
//create an instance of the FormPrintView_ADG component containing the datagrid
var thePrintView:FormPrintView_ADG = new FormPrintView_ADG();
//add the component to my application
FlexGlobals.topLevelApplication.addChild(thePrintView);
//load the datagrid
thePrintView.printDataGrid.dataProvider = xmlListCollection.copy();
//format the datagrid
thePrintView.printDataGrid.width = printJob.pageWidth-45; //set a left margin for the dg in a right justiified vBox
thePrintView.printDataGrid.height = printJob.pageHeight-73; //page adjusted for header title and date
thePrintView.printDataGrid.setStyle("fontSize", 8);
thePrintView.printDataGrid.columns = advDG.columns;
thePrintView.printDataGrid.setStyle("fontFamily", 'Times');
thePrintView.printDataGrid.setStyle("color", 000000);
//set the header text
thePrintView.headerText.height = 45;
thePrintView.headerText.width = printJob.pageWidth-20;
thePrintView.headerText.text = "\r"+headerText;
//add the first page to the print job
printJob.addObject(thePrintView, FlexPrintJobScaleType.NONE);
while (thePrintView.printDataGrid.validNextPage) {
// Move the next page of data to the top of the PrintDataGrid and add it to the printjob
thePrintView.printDataGrid.nextPage();
printJob.addObject(thePrintView, FlexPrintJobScaleType.NONE);
}
//print it and remove the component from my application
printJob.send();
FlexGlobals.topLevelApplication.removeChild(thePrintView);
}
}
Here is the vBox component I am using...
<?xml version="1.0"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
horizontalAlign="right"
creationComplete="init();">
<mx:Script>
<![CDATA[
[Bindable] private var date:String;
private function init():void{
date = new Date().toString();
date = df.format(date);
}
]]>
</mx:Script>
<mx:DateFormatter id="df" formatString="EEEE, MMMM D, YYYY"/>
<!-- this header can contain a title or be left blank to create a top margin -->
<mx:TextArea id="headerText" borderThickness="0" color="#000000" fontWeight="bold"
textAlign="center" textDecoration="none"/>
<!-- date label. set visible to false in the calling function if not needed -->
<mx:Label color="#000000" fontSize="8" fontStyle="normal" fontWeight="normal" text="{date}"/>
<!-- the data grid -->
<mx:PrintAdvancedDataGrid id="printDataGrid"/>
</mx:VBox>