A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] Create tables with AlivePDF

Threaded View

  1. #4
    Senior Member
    Join Date
    Dec 2010
    Posts
    121

    Thumbs up Problem solved - Table created

    This is my code in Flash, not Flex. Hope it helps anyone else struggling to get started. I'm using AlivePDF 0.1.5RC.

    Grid feature in AlivePDF is introduced in this page: http://alivepdf.bytearray.org/?p=25

    In this project, I used DataGrid to make the table inside Flash, then passed it into another PDF generation AS. So you should also read about DataGrid and try it out:
    http://www.adobe.com/livedocs/flash/...=00000436.html
    (wait for the page to load, then click "Creating an application with the DataGrid" - navigate to creating with Actionscript and start reading).

    I also used this thread to learn to make columns in AlivePDF (http://forums.esri.com/Thread.asp?c=158&f=2421&t=302275)

    I make a separate PDF Generation file called PDFGeneration.as:

    Actionscript Code:
    package
    {
        import flash.utils.ByteArray;
        import flash.net.FileReference;
       
        import org.alivepdf.pdf.PDF;
        import org.alivepdf.layout.Orientation;
        import org.alivepdf.layout.Unit;
        import org.alivepdf.layout.Size;
        import org.alivepdf.layout.Align;
        import org.alivepdf.pages.Page;
        import org.alivepdf.fonts.FontFamily;
        import org.alivepdf.display.Display;
        import org.alivepdf.fonts.Style;
        import org.alivepdf.fonts.IFont;
        import org.alivepdf.colors.RGBColor;
        import org.alivepdf.saving.Method;
        import org.alivepdf.fonts.CoreFont;
        import org.alivepdf.data.Grid;
        import org.alivepdf.data.GridColumn;
        import org.alivepdf.pages.Page;
       
        import fl.controls.DataGrid;
        import fl.data.DataProvider;
           
        public class PDFGeneration
        {
            private var myPDF:PDF;
            //choose your font here    
            private var boldFont:IFont = new CoreFont(FontFamily.HELVETICA_BOLD);
            private var normalFont:IFont = new CoreFont(FontFamily.ARIAL);
           
           
            //table stuff
            private var grid:Grid;
            public var pdfDataGrid:DataGrid = new DataGrid(); //datagrid to be passed into here
           
            //you need to set up headerText so if your grid go over board, the header will
            //repeat in the next page. Option to repeat header can be switched in addGrid().
            private var cT:GridColumn = new GridColumn("t", "t", 22, "L", "L");
            private var cAx:GridColumn = new GridColumn("Ax", "Ax", 22, "L", "L");
            private var cAy:GridColumn = new GridColumn("Ay", "Ay", 22, "L", "L");
            private var cVx:GridColumn = new GridColumn("Vx", "Vx", 22, "L", "L");
            private var cVy:GridColumn = new GridColumn("Vy", "Vy", 22, "L", "L");
            private var cX:GridColumn = new GridColumn("X", "X", 22, "L", "L");
            private var cY:GridColumn = new GridColumn("Y", "Y", 22, "L", "L");
            private var columns:Array = new Array(cT, cAx, cAy, cVx, cVy, cX, cY);
           
            public function PDFGeneration()
            {
                //nothing here
            }

            private function roundX(toBeRounded:Number, decimalPlaces:Number):Number
            {
                return (int(toBeRounded * Math.pow(10,decimalPlaces)) / Math.pow(10,decimalPlaces));
            }
           
            public function savePDF():void
            {
                myPDF = new PDF(Orientation.PORTRAIT,Unit.MM, Size.A4);
               
                myPDF.setDisplayMode(Display.FULL_WIDTH);
               
                //page 1 starts the table of results
                myPDF.addPage();
                grid = new Grid(pdfDataGrid.dataProvider.toArray(), 271, 265, new RGBColor(0xCCCCCC), new RGBColor(0xFFFFFF), false, new RGBColor(0x0), 1, "0 j");
                grid.columns = columns;
               
                myPDF.setFont(normalFont, 10, false);
                myPDF.textStyle(new RGBColor(0x0), 1);
                myPDF.addGrid(grid, 12.7, 18.7);
               
                //save PDF
                var bytes:ByteArray = myPDF.save(Method.LOCAL);
                var f:FileReference = new FileReference();
                f.save(bytes, "SimulationResults.pdf");
            }
        }
    }

    Main.as exportPDF sections
    Actionscript Code:
    package
    {
        //other classes was imported here
       
        import fl.controls.DataGrid; //you need to add DataGrid component to the library before using this class
        import fl.controls.dataGridClasses.DataGridColumn;
        import fl.data.DataProvider;
       
        import PDFGeneration;
       
        public class Main extends MovieClip
        {
            //pdf variables
            public var exportPDF:PDFGeneration = new PDFGeneration();
           
            //variables that work and stay here
            private var values_array:Array = new Array(); //represents the data provider array
            private var myGc:DataGridColumn = new DataGridColumn();
            private var myDg:DataGrid = new DataGrid();

            //variables to calculate values like aX, aY was declared here

            private var myTimerNo:Timer;

            private var aniNo:ProjectileNo = new ProjectileNo();
           
        //export PDF and pass in datagrid variable
            private function export(e:MouseEvent):void
            {
                exportPDF.pdfDataGrid = myDg;
               
                exportPDF.savePDF();
            }
        //dataGrid function
            private function addSheet():void //just a visual datagrid on the SWF for cross-checking
            {
                myDg.columns = ["t", "Ax", "Ay", "Vx", "Vy", "X", "Y"];
                myDg.move(596, 35.75);
                myDg.dataProvider = new DataProvider(values_array);
                myDg.rowCount = myDg.length;
                myDg.setSize(389, 307);
                addChild(myDg);
            }

            private function startSimNo(e:TimerEvent):void //just a list of values
            {
                aX = 0;
                aY = -9.8;

                timePassed = e.target.currentCount * interval;
                myDot.x = 0;
                myDot.y = 0;
                container_mc.addChild(myDot);

                xPos = vX * timePassed;
                yPos = aniNo.findYNo(y0,xPos,lAngle,iVelocity);
               
                if (yPos >= bottom)
                {
                    finStart();
                }

                myDot.x = xPos;
                myDot.y = yPos;

                container_mc.addChild(trail);
                trail.graphics.lineTo(xPos, yPos);
               
                //values to use in local datagrid
                //roundX is a decimal places fixing function
                values_array.push({t:roundX(timePassed,2), Ax:roundX(aX,2), Ay:roundX(aY,2), Vx:roundX(vX,2), Vy:roundX(vY,2), X:roundX(xPos,2), Y:roundX(-yPos,2)});
            }
           
            //start the constuctor and functions that handle events here - works great
            public function Main()
            {  
                addEventListener(Event.ADDED_TO_STAGE, init);
            }

            public function init(e:Event):void
            {
                removeEventListener(Event.ADDED_TO_STAGE, init);
                export_btn.addEventListener(MouseEvent.CLICK, export);
            }
        }
    };

    The width for your table is actually defined by the sum of the column's width, using GridColumn. I find that the width inside Grid(..) doesn't really do anything. It's just there to keep everything intact.
    Last edited by flashmed; 01-19-2011 at 12:04 AM. Reason: slight error with code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center