Designing Custom Document Viewer using ActionScript 3

Designing a document viewer using ActionScript 3 requires Flash and
ActionScript 3 programming skills. For developing of your viewer you may use
Adobe Flash CS Professional or Adobe Flex.
Print2Flash SDK contains a sample of a simple ActionScript 3 document viewer
developed in Adobe Flash CS3 Professional. It can be imported into Adobe Flash
CS4 Professional format as well. We recommend using this sample as a starting
point when developing your own viewer. The description below deals with this
sample code so refer to it for full details. If you want to develop a viewer
using Flex, you may use code scraps from the sample and from below in this help topic.
General viewer movie requirements
The viewer should be a Flash movie which is published for Flash Player
version 9 or higher and which uses ActionScript 3. The viewer may contain any
number of frames. When Print2Flash generates a document, it takes your viewer
movie, appends document pages to it and writes the result as the
output document Flash file. It is the responsibility of the viewer movie to
attach appended pages and use them for displaying of document interface. The SDK
sample uses the standard ScrollPane component for displaying document pages in
an area where a continuous stack of document pages can be scrolled. Of course,
you may use other methods for document display attaching document pages not to
ScrollPane component but to other movie clips.
When generating output Flash document, Print2Flash modifies your viewer movie
by adding additional frames to it, one frame per each document page. So keep in
mind that the number of frames when your viewer movie is executed is greater
than the number of frames in your viewer at design time by the number of document pages.
Your viewer should not stop the movie in the last frame and should allow
continuous movie execution past the last frame. Print2Flash does not stop the
movie after the last appended frame as well so your viewer movie loops by
default. You may stop the movie when execution is looped back to the first frame using the code like this:
var secondtime
if (secondtime) return;
addFrameScript(totalFrames-1,stop);
secondtime=true;
|
You may place this code, for example, at the beginning of your first frame
script.
Attaching page movies
Each page movie is represented by an ActionScript class inherited from MovieClip
class associated with a library symbol. To create a page movie instance, you may use
this function:
function AttachPageMovie(pageno:Number,parent:DisplayObjectContainer) {
var id="Page"+pageno
var mc
try {
var AssetClass:Class = getDefinitionByName(id) as Class;
mc = new AssetClass();
}
catch(e:Error) {
}
if (mc) parent.addChild(mc);
return mc;
}
|
The function accepts two parameters:
- pageno - the number of page to create (starts from 1);
- parent - the parent movie to which to attach the created page movie as a
child.
The function returns a reference to the created MovieClip instance of the
page movie. If a movie cannot be created, undefined is returned.
Print2Flash appends page movies to the viewer movie in such a way so that
document can be opened and viewed by users before the Flash file is fully
loaded. This makes user interface more responsive and allows users to start
viewing the document sooner without having to wait until the document file is
loaded in full. As a result of this, when executing the code in your first
frames for the first time, no pages are available yet. They become available
later as pages code is being loaded by the Flash Player. Because of this we
recommend to start a timer and attach pages in timer function until all pages
are loaded as demonstrated in this code:
var myTimer:Timer = new Timer(100);
myTimer.addEventListener("timer", CheckLoadedPages);
var loadedPages=0;
myTimer.start();
function CheckLoadedPages(event:TimerEvent):void {
ScanPages(loadedPages+1);
if (loadedPages>=parseInt(GetSetting("PageNum"))) {
myTimer.stop();
myTimer=null;
}
}
function ScanPages(startpage) {
var pageno=startpage;
do {
var movie=AttachPageMovie(pageno,parent);
var validpage=typeof(movie)!="undefined";
if (validpage) {
// Process page here: scale, position, etc.
loadedPages=pageno;
}
pageno++;
}
while(validpage);
}
|
This code assumes that parent variable is set to a reference to the
parent movie. After successful attachment you may process the page instance:
scale, position it, etc. as indicated in the code comment.
Document settings
Along with page movies Print2Flash stores additional settings in the document
file which may help the viewer in document rendering. The list of available
parameters is contained in Custom Document Viewer Settings help topic. To read these settings you need to
declare and execute this code:
var settingsArr:ByteArray=AttachBinData("Settings");
var settings:XML=new XML(settingsArr.readUTFBytes(settingsArr.length));
function AttachBinData(id):ByteArray {
var ba
try {
var AssetClass:Class = getDefinitionByName(id) as Class;
ba = new AssetClass();
}
catch(e) {
}
return ba
}
function GetSetting(name){
var val=settings.attribute(name);
return val;
}
|
Then you may use declared GetSetting function to retrieve a setting value
passing it the setting name as a parameter. For example, to get the total number
of pages in the document, you may use this code:
var totalPages:Number=parseInt(GetSetting("PageNum"));
|
Note that all values are returned by GetSetting function as strings so you may need to convert them
to other types as demonstrated in the previous sample with standard parseInt
function.
|