Using Print2Flash Document API from Flash with ActionScript 3

Print2Flash documents can be loaded into other Flash movies created in Adobe
Flash CS3 Professional with ActionScript 3 support and controlled using
Print2Flash Document API.
To facilitate this task, you may make use of Print2FlashDoc class which is included in Print2Flash SDK.
First, copy the folder named Print2Flash to a directory located at class path
of your project It can be the root directory of your project.
Then, you may use the code such as the following to load a Print2Flash document:
import Print2Flash.*;
var P2FDoc:Print2FlashDoc=new Print2FlashDoc("sample.swf",10,50,500,350,this);
|
The meanings of Print2FlashDoc constructor parameters (from left to right)
are:
- url - path of Print2Flash document to load;
- x - x coordinate of the Print2Flash document movie;
- y - y coordinate of the Print2Flash document movie;
- width - width of Print2Flash document movie;
- height - height of Print2Flash document movie;
- parent- parent object of Print2Flash document movie.
Note that to load a document in Flash and access it via Print2Flash Document
API you should clear "Disable
Print2Flash Document API support" option when converting the document.
Then you may call any Document API functions using the created Print2FlashDoc
class instance reference. For example:
P2FDoc.setCurrentPage(5);
|
Invoking functions that return values is a bit tricky in ActionScript 3 as interaction
between ActionScript 3 and Print2Flash documents is asynchronous. It means that you should
break your function calling code into two parts: the first part for calling the
function and another part for obtaining the result. When
calling such functions you should pass an additional argument at first position
which must denote the function that will receive the return value. For example,
to call getCurrentPage
function you should use the code such as this:
public function ongetCurrentPage(page:Number):void {
trace(page);
}
P2FDoc.getCurrentPage(ongetCurrentPage);
|
Here ongetCurrentPage is the function that receives the result of the
getCurrentPage function
which is passed to it as a parameter. When calling
getCurrentPage function,
the first argument denotes ongetCurrentPage function as the function to
call for receiving the return value.
To handle events fired by Print2Flash documents, you should register an event
handler using standard addEventListener method. For example, to handle
onPageChanged
event you need to declare the event handler like this:
function onPageChanged(e:PageChangedEvent) {
trace(e.page.toString())
}
P2FDoc.addEventListener("onPageChanged", onPageChanged);
|
The event processing code must reference an object named event which
holds the event detailed information. Each event has a special type of event
object. These types are detailed in the table below.
|
Event |
Event object type |
Event object properties |
| onPageChanged |
Print2Flash.PageChangedEvent |
page:Number - page number |
| onZoomChanged |
Print2Flash.ZoomChangedEvent |
zoom:Number - zoom level |
| onSelection |
Print2Flash.Selection |
selection:Object - text selection described
by object of SelectionRange
type |
| onToolChanged |
Print2Flash.ToolChangedEvent |
tool:String - tool represented by text as
in getCurrentTool
function |
| onVisibleAreaChanged |
Print2Flash.VisibleAreaEvent |
area:Object - visible area described by
object of VisibleArea type |
| onLoaded |
flash.events.Event |
No properties |
| onUnloadCompletedEvent |
Print2Flash.UnloadCompletedEvent |
No properties |
If you want an event handler to stop receiving event notifications, you need
to remove the handler using removeEventListener method:
P2FDoc.removeEventListener("onPageChanged", onPageChanged);
|
Print2FlashDoc component exposes an event additional to
Print2Flash Document API events named
onLoaded. It is called when the document is ready to be accessed by API
methods. You should not call any methods of Print2FlashDoc component before
onLoaded event is fired.To remove (unload) a loaded document properly, you should call unload
method of the Print2FlashDoc class. This is to ensure that all resources used by
the document are freed. If you want to load another document reusing the same
Print2FlashDoc class instance, you may not do this until onUnloadCompletedEvent
event is fired. If you try to load a document before this event is fired, you'll
get an exception.
Print2Flash SDK contains a sample of
embedding a Print2Flash document into a Flash movie which uses ActionScript 3.
|