Sunday, 20 September 2015

ActionScript 3.0 Opening a URL and downloading a PDF file

To open any external link from a flash document we use the below code:



package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;
    import flash.text.TextField;
    import flash.text.TextFormat;
   
    /**
     * ...
     * @author shrikant
     */
    public class OpenUrl extends Sprite
    {
        public function OpenUrl() {
            // create a sprite object
            var buttonSprite:Sprite = new Sprite();
            buttonSprite.graphics.lineStyle(1, 0x000000);
            buttonSprite.graphics.beginFill(0xFF0000, 0.2);
            buttonSprite.graphics.drawRoundRect(0, 0, 200, 50, 30, 30);
            buttonSprite.x = 10;
            buttonSprite.y = 20;
            buttonSprite.buttonMode = true;
            buttonSprite.graphics.endFill();
            addChild(buttonSprite);
           
            // create a textfield object
            var btnTF:TextField = new TextField();
            var btnFormat:TextFormat = new TextFormat("Arial", 17, 0x000000, true);
            btnTF.defaultTextFormat = btnFormat;
            btnTF.text = "Click here!";
            btnTF.x = (buttonSprite.width / 2) - (btnTF.width / 2.2);
            btnTF.y = (buttonSprite.height / 2) - (btnTF.height / 8);
            btnTF.selectable = false;
            btnTF.border = false;
            btnTF.width = 85;
            btnTF.height = 20;
            btnTF.mouseEnabled = false;
            buttonSprite.addChild(btnTF);
           
            // add an eventlistener to open a URL
            buttonSprite.addEventListener(MouseEvent.CLICK, openAdobeUrl);
        }
       
        // function which opens a URL
        public function openAdobeUrl(evt:MouseEvent):void {
            var adobeURL:URLRequest = new URLRequest("http://www.adobe.com");
            navigateToURL(adobeURL, "_blank");
        }
       
    }
   
}


You can use the same technique to open a PDF file. The below code does the same.

package
{
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;
    /**
     * ...
     * @author shrikant
     */
    public class OpenPDF extends Sprite
    {
        // embed the PDF icon
        [Embed(source = "../lib/pdf_icon.jpg")]
        private var layer0Class:Class;
        private var layer0:Bitmap = new layer0Class();
       
        public function OpenPDF()
        {
            // create a button sprite
            var btnSprite:Sprite = new Sprite();
            btnSprite.buttonMode = true;
            addChild(btnSprite);
           
            layer0.width = 50;
            layer0.height = 50;
            btnSprite.addChild(layer0);
           
            // add an eventlistener to open PDF file
            btnSprite.addEventListener(MouseEvent.CLICK, openPDF);
        }
       
        // function to open PDF file
        public function openPDF(evt:MouseEvent):void {
            var pdfOpen:URLRequest = new URLRequest("../lib/myPDF.pdf");
            navigateToURL(pdfOpen);
        }
       
    }

}

If you are using a Flash IDE follow the below steps:

Opening a URL

1) Create a new ActionScript 3.0 Project
2) Create a button symbol for example click me or import it from the library
3) Give an instance name to the button symbol for example myButton
4) Create a new layer name it actions and write the below code

import flash.events.MouseEvent;
import flash.net.URLRequest;

myButton.addEventListener(MouseEvent.CLICK, onBtnClick);

function onBtnClick(evt:MouseEvent):void {
    var adobeURL:URLRequest = new URLRequest("http://www.adobe.com");
    navigateToURL(adobeURL,"_blank");
}

Downloading a PDF file

1) Create a new ActionScript 3.0 Project
2) Create a button symbol with PDF Download Text or import it an PDF download icon and convert it to a button symbol.
3) Give an instance name to the button symbol for example pdfButton
4) Create a PDF document which needs to be opened.
5) Create a new layer name it actions and write the below code

// open PDF
pdfButton.addEventListener(MouseEvent.CLICK, onPDFClick);

function onPDFClick(evt:Event):void
{
    var pdfOpen:URLRequest = new URLRequest("myPDF.pdf");
    navigateToURL(pdfOpen,"_blank");
}
// end openPDF

Note: If the PDF file is in a folder you also need to specify the folder name viz "docs/myPDF.pdf"

No comments:

Post a Comment