Using Print2Flash Document API from Flex

Print2Flash documents can be embedded into Adobe Flex applications and controlled
from them using Print2Flash Document API.
To facilitate this task, you may make use of Print2FlashDoc Flex
component which is included in Print2Flash SDK.
First, copy the component folder named Print2Flash to a directory located at
class path of your Flex project It can be the root directory of your
project.
Then, Print2FlashDoc component should appear in the Custom group in the
Components window of Adobe Flex Builder. In MXML editor's Design mode drag this
component into the layout and specify its position and size. In the Flex
Properties window click Alphabetical view button at its top and enter the URL of
the Print2Flash document you want to display in the source property.
After that you may launch your application and the Print2Flash document
should load into it and appear at the space you placed Print2FlashDoc component
at.
To control the document you should give a name to the Print2FlashDoc
component instance. This can be done by selecting Print2FlashDoc component
instance in the layout and setting its id property in the Flex Properties
window.
To invoke functions of
Print2Flash Document API, use the name
you have given to the Print2FlashDoc instance as an object variable and invoke
Print2Flash Document API functions as
methods of this object. For example, to invoke
setCurrentPage
function on the instance named doc use the code such as the following:
doc.setCurrentPage(3)
Invoking functions that return values is a bit tricky in Flex as interaction
between Flex 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)
}
doc.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 enter the event
handling code in the respective event properties of the Print2FlashDoc component
instance. For this, select Print2FlashDoc component instance in the layout and
enter event handling code in its event properties in the Flex Properties window.
For example, to process
onZoomChanged event enter the processing code in the onZoomChanged property of
the Print2FlashDoc component instance.
The event processing code may 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 |
For example, in
onZoomChanged event processing code you may have the code which outputs the
new zoom level to the console window like this:
trace(event.zoom.toString())
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.
Print2Flash SDK contains a sample of
embedding a Print2Flash document into the Flex application.
|