Printing a DataGrid using FlexPrintJob Class

6 Jun
2008
Delicious

Here is how you can print a DataGrid in Flex using the FlexPrintJob Class

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	 creationComplete="initApp()">
	<mx:Script>
		<![CDATA[
			import mx.printing.FlexPrintJob;
			import mx.collections.ArrayCollection;
 
 
			[Bindable]
			private var myCollection:ArrayCollection = new ArrayCollection();
			private function initApp():void
			{
				for(var i:int=0;i<200;i++)
				{
					myCollection.addItem({row: i, data:"Item "+ i});
 
				}
			}
			private function print():void
			{
				var printJob:FlexPrintJob = new FlexPrintJob();
				//start the print job
				if(printJob.start())
				{
 
					printJob.addObject(printDataGrid);
					while(true)
					{
						printDataGrid.nextPage();
						if(!printDataGrid.validNextPage)
						{
							printJob.addObject(printDataGrid);
							break;
						}
					}
					printJob.send();
				}
			}
 
		]]>
	</mx:Script>
 
 
	<mx:DataGrid id="dataGrid" dataProvider="{myCollection}" height="300" width="200">
		<mx:columns>
			<mx:DataGridColumn dataField="row" headerText="Row Number"/>
			<mx:DataGridColumn dataField="data" headerText=" Item #"/>
		</mx:columns>
	</mx:DataGrid>
 
	<mx:PrintDataGrid id="printDataGrid" dataProvider="{myCollection}"  x="294"
		 visible="false" includeInLayout="false">
		<mx:columns>
			<mx:DataGridColumn dataField="row" headerText="Row Number"/>
			<mx:DataGridColumn dataField="data" headerText=" Item #"/>
		</mx:columns>
	</mx:PrintDataGrid>
	<mx:Button x="68" y="308" label="Print" click="print()"/>
 
 
 
</mx:Application>
Incoming Search Terms :flexprintjob , flex datagrid print , print datagrid in flex , flex print datagrid , print datagrid flex , printing datagrid in flex Incoming Search Terms :flexprintjob , flex datagrid print , print datagrid in flex , flex print datagrid , print datagrid flex , printing datagrid in flex

Share this story with a friend !
Join SherifAbdou's Rss Feed Stumble This Article! Add this to your Delicious Bookmarks! Digg this story ! Add this story to Reddit ! Tweet This Brush


4 Responses to Printing a DataGrid using FlexPrintJob Class

Avatar

Printing a DataGrid using FlexPrintJob Class

June 6th, 2008 at 6:56 pm

[...] bw wrote an interesting post today onHere’s a quick excerpt[CDATA[ import mx.printing.FlexPrintJob; import mx.collections.ArrayCollection; [Bindable] private var myCollection:ArrayCollection = new ArrayCollection(); private function initApp():void { for(var i:int=0;i [...]

Avatar

ScotNo Gravatar

June 8th, 2008 at 3:49 pm

It didn’t work for me, all that printed was a few sheets with “Row Number” and “Item #” at the top, the print was also very very large. Did I need to do more than cut and paste?

Avatar

SherifNo Gravatar

June 8th, 2008 at 5:43 pm

I looked at it, and I think there is some sort of bug. Make it visible, then move around the header in the printDataGrid then it should work

Avatar

CorneliaNo Gravatar

August 14th, 2009 at 8:25 am

Hi, I have a small question (very big for me). How can I print two grids one after another: when the first grid is finished, the second one is added right after, not on a diffrent page. Thanks in advance! :)

Comment Form