2 Replies Latest reply on Aug 31, 2011 3:52 AM by Kamil_Pullo

    Add Image Dynamically

    Kamil_Pullo

      Hi, can anyone help me to add an image dynamically?


      i tried this way....

       

       

            private var loader:Loader = new Loader();
            loader.load(new URLRequest("20332.png"));
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedPicture);
      

       

              private function loadedPicture(e:Event):void
              {   
                  var pdf:PDF = configPdf();
                  //thats right?
                  pdf.addImage(loader.content);
                  //or this?
                  var imageByte:ByteArray = loader.content as ByteArray;
                  pdf.addImageStream(imageByte, ColorSpace.DEVICE_RGB, null , 0, 0, 50, 50, 0, 1,"Normal",null );
              }
      

       

      Thanks for help

        • 1. Re: Add Image Dynamically
          saisri2k2 Level 4

          I dont know what the PDF does,

           

          but you can do

           

          var img:Image = new Image()

          img.source = byteArray;

          this.addElement(img);//or try addChid()

          • 2. Re: Add Image Dynamically
            Kamil_Pullo Level 1

            The PDF create my PDF dokument(AlivePDF).... The problem is that the complete event is too late. The pdf is saved before thera a passing the complete event. When i take pdf.addImage bevore save PDF then loader.content is null. Ideas?

                      private function clickHandler(event:Event):void {
                           var fileRef:FileReference = new FileReference();
                           fileRef.addEventListener(Event.COMPLETE, saveComplete);
                           
                           if ( action == "save" ) {
                                /* Save as a file */
                                if ( charSet == 'UTF16LE' ) {
                                     fileRef.save( strToUTF16LE(clipText), fileName );
                                } else {
                                     fileRef.save( strToUTF8(clipText), fileName );
                                }
                           } else if ( action == "pdf" ) {
                                /* Save as a PDF */
                                  pdf= configPdf();
                                 //I must add the Image bevore save the pdf but here is loader.content null
                                 fileRef.save( pdf.save( Method.LOCAL ), fileName );
                      
                           } else {
                                /* Copy the text to the clipboard. Note charset and BOM have no effect here */
                                System.setClipboard( clipText );
                                ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'complete', clipText );
                           }
                      }
            

             

                      private function configPdf():PDF
                      {
                           var
                                pdf:PDF,
                                i:int, iLen:int,
                                splitText:Array    = clipText.split("--/TableToolsOpts--\n"),
                                opts:Array         = splitText[0].split("\n"),
                                dataIn:Array       = splitText[1].split("\n"),
                                aColRatio:Array    = getProp( 'colWidth', opts ).split('\t'),
                                title:String       = getProp( 'title', opts ),
                                message:String     = getProp( 'message', opts ),
                                orientation:String = getProp( 'orientation', opts ),
                                size:String        = getProp( 'size', opts ),
                                iPageWidth:int     = 0,
                                dataOut:Array      = [],
                                columns:Array      = [],
                                headers:Array,
                                y:int = 0,
                                           image:String = getProp('image',opts);
                               /* Create the PDF */
                               pdf = new PDF( Orientation[orientation.toUpperCase()], Unit.MM, Size[size.toUpperCase()] );
                               pdf.setDisplayMode( Display.FULL_WIDTH );
                               pdf.addPage();
                               iPageWidth = pdf.getCurrentPage().w-20;
                               pdf.textStyle( new RGBColor(0), 1 );
                            
                                 //IMAGE Loader---------------
                                 var request:URLRequest = new URLRequest();
                                 request.url = "20493.jpg";
                                 loader.load(request);
                                 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedPicture);
                                //Here i must add a Image but loader.content is null
            
                               /* Add the title / message if there is one */
                               pdf.setFont( new CoreFont(FontFamily.HELVETICA), 14 );
                               if ( title != "" )
                               {
                                    pdf.writeText(11, title+"\n");
                               }
                               
                               pdf.setFont( new CoreFont(FontFamily.HELVETICA), 11 );
                               if ( message != "" )
                               {
                                    pdf.writeText(11, message+"\n");
                               }
                               
                               /* Data setup. Split up the headers, and then construct the columns */
                               for ( i=0, iLen=dataIn.length ; i<iLen ; i++ )
                               {
                                    if ( dataIn[i] != "" )
                                    {
                                         dataOut.push( dataIn[i].split("\t") );
                                    }
                               }
                               headers = dataOut.shift();
                               
                               for ( i=0, iLen=headers.length ; i<iLen ; i++ )
                               {
                                    columns.push( new GridColumn( " \n"+headers[i]+"\n ", i.toString(), aColRatio[i]*iPageWidth, 'C' ) );
                               }
                               
                               var grid:Grid = new Grid(
                                    dataOut,                  /* 1. data */
                                    iPageWidth,               /* 2. width */
                                    100,                      /* 3. height */
                                    new RGBColor (0xE0E0E0),  /* 4. headerColor */
                                    new RGBColor (0xFFFFFF),  /* 5. backgroundColor */
                                    true,                     /* 6. alternateRowColor */
                                    new RGBColor ( 0x0 ),     /* 7. borderColor */
                                    .1,                       /* 8. border alpha */
                                    null,                     /* 9. joins */
                                    columns                   /* 10. columns */
                               );
                               pdf.addGrid( grid, 0, y );
                               return pdf;
                      }
            
                   private function loadedPicture(e:Event):void
                   {
                       //Thats too late
                       pdf.addImage(loader.content);
                       ExternalInterface.call("console.log","loaded picture");
                   }