<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="initData()"> <mx:Script> <![CDATA[ import mx.controls.dataGridClasses.DataGridColumn; import org.alivepdf.fonts.CoreFont; import mx.collections.ArrayCollection; import org.alivepdf.colors.IColor; import org.alivepdf.colors.RGBColor; import org.alivepdf.data.Grid; import org.alivepdf.data.GridColumn; import org.alivepdf.saving.Method; import org.alivepdf.display.Display; import org.alivepdf.layout.Size; import org.alivepdf.layout.Unit; import org.alivepdf.layout.Orientation; import org.alivepdf.pdf.PDF; private var dgArray:Array = [ {Artist:'Pavement', Album:'Slanted and Enchanted', Price:11.99}, {Artist:'Pavement', Album:'Brighten the Corners', Price:11.99}, {Artist:'test2', Album:'junk1', Price:11.99}, {Artist:'test2', Album:'junk2', Price:22.99}, {Artist:'test3', Album:'junk3', Price:11.99}, {Artist:'test3', Album:'junk4', Price:11.99}, {Artist:'test4', Album:'junk1 and Enchanted', Price:35.99}, {Artist:'test4', Album:'Slanted and junk1', Price:6.99}, {Artist:'test5', Album:'Slanted junk1', Price:89.99}, {Artist:'test5', Album:'junk1 Enchanted', Price:33.12}]; [Bindable] public var dgArrayColl:ArrayCollection; private function initData():void { dgArrayColl=new ArrayCollection(dgArray); } private function myLabelFunc(data:Object, column:DataGridColumn):String { return "$" + data.Price; } private function sendToPDF():void { var PDFcolumns:Array = getPDFColumns(dg); var resultsArray:Array = dg.dataProvider.toArray(); formatPDFGridData(resultsArray, dg); var pdf:PDF = new PDF(Orientation.PORTRAIT,Unit.MM,Size.A4); pdf.addPage(); pdf.setAutoPageBreak(false,1); pdf.setDisplayMode ( Display.FULL_PAGE ); pdf.setFont(new CoreFont(),10); pdf.textStyle(new RGBColor(0x000000)); var rgbWhite:IColor = new RGBColor(0xFFFFFF); var rgbBlack:IColor = new RGBColor(0x000000); var rgbGray:IColor = new RGBColor(0xCCCCCC); var grid:Grid = new Grid(resultsArray,0,dg.height,rgbGray,rgbWhite,false,rgbBlack); grid.columns = PDFcolumns; pdf.addGrid(grid,25,10); pdf.save(Method.REMOTE, "http://localhost/createPDF/create.aspx", Download.ATTACHMENT, "report.pdf"); } // end function sendToPrinter private function getPDFColumns(datagrid:DataGrid):Array { // This will create an array suitable for use as columns in AlivePDF // The columns are based on a datagrid's columns. // If the datagrid column is visible it will be included. //The width of each column is calculated assuming mm are the units used in the pdf. //1 pt = 0.35 mm //1 mm = 2.83 pt var PDFcolumns:Array = new Array; var dataGridColumn:DataGridColumn; //loop through each datagridcolumn in the datagrid for( var i:Number = 0; i < datagrid.columns.length; i++ ) { dataGridColumn = datagrid.columns; if (dataGridColumn.visible) { // add to the pdfcolumn array var gridColumn:GridColumn = new GridColumn(dataGridColumn.headerText, dataGridColumn.dataField, dataGridColumn.width * 0.35); PDFcolumns.push(gridColumn); } } return PDFcolumns; } private function formatPDFGridData(arr:Array, datagrid:DataGrid):void { var dataGridColumn:DataGridColumn; // loop through the array for (var i:int = 0; i<arr.length; i++) { //go through each datagridColumn in the datagrid for( var iCols:Number = 0; iCols < datagrid.columns.length; iCols++ ) { dataGridColumn = datagrid.columns[iCols]; // If there is a labelfunction on the column, // update the data in the array using its return value if (dataGridColumn.labelFunction != null) { arr[dataGridColumn.dataField] = dataGridColumn.labelFunction(arr,dataGridColumn); } } } } ]]> </mx:Script> <mx:DataGrid id="dg" dataProvider="{dgArrayColl}" width="450"> <mx:columns> <mx:DataGridColumn dataField="Artist" width="100"/> <mx:DataGridColumn dataField="Album" /> <mx:DataGridColumn dataField="Price" width="100" visible="true" labelFunction="myLabelFunc"/> </mx:columns> </mx:DataGrid> <mx:Button label="PDF" click="sendToPDF()"/> </mx:Application>
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.