写在开头:
本文主要资料参考来自mark的《Flexpaper二次开发入门教程》,在此鸣谢!该教程写得还是很详细,初次接触flexpaper的可以先看看该教程。
下面主要谈谈二次开发flexpaper的过程中的一些总结。
我的项目名称为Diyflexpaper,修改Diyflexpaper.mxml如下:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:fp="com.devaldi.controls.flexpaper.*" layout="absolute" width="100%" height="100%" applicationComplete="initApp();"> <mx:Metadata> [ResourceBundle("FlexPaper")] [Event(name="onDocumentLoaded", type="com.devaldi.events.DocumentLoadedEvent")] [Event(name="onLoadingProgress", type="flash.events.ProgressEvent")] [Event(name="onDocumentLoadedError", type="flash.events.ErrorEvent")] [Event(name="onCurrPageChanged", type="com.devaldi.events.CurrentPageChangedEvent")] [Event(name="onDocumentLoading", type="flash.events.Event")] [Event(name="onExternalLinkClicked", type="com.devaldi.events.ExternalLinkClickedEvent")] [Event(name="onDocumentPrinted", type="com.devaldi.events.DocumentPrintedEvent")] [Event(name="onPageLoaded", type="com.devaldi.events.PageLoadedEvent")] [Event(name="onPageLoading", type="com.devaldi.events.PageLoadingEvent")] [Event(name="onErrorLoadingPage", type="com.devaldi.events.ErrorLoadingPageEvent")] </mx:Metadata> <mx:Script> <![CDATA[ import mx.controls.Alert; import com.devaldi.events.CurrentPageChangedEvent; import com.devaldi.events.CursorModeChangedEvent; import com.devaldi.events.DocumentLoadedEvent; import com.devaldi.events.DocumentPrintedEvent; import com.devaldi.events.ErrorLoadingPageEvent; import com.devaldi.events.ExternalLinkClickedEvent; import com.devaldi.events.FitModeChangedEvent; import com.devaldi.events.ScaleChangedEvent; import com.devaldi.events.ViewModeChangedEvent; public var _aid:Number = 0;//文档ID [Bindable] public var _Scale:Number = 1;//缩放比例 [Bindable] public var _EncodeURI:Boolean = true; [Bindable] public var _SwfFile:String = "";//SWF文件路径 [Bindable] public var _ZoomTransition:String = "easeOut"; [Bindable] public var _ZoomTime:Number = 0.6; [Bindable] public var _ZoomInterval:Number = 0.1; [Bindable] public var _MinZoomSize:Number = 0.3; [Bindable] public var _MaxZoomSize:Number = 5; [Bindable] public var _FitPageOnLoad:Boolean = false;//加载后适合高度 [Bindable] public var _FitWidthOnLoad:Boolean = false;//加载后适合宽度 [Bindable] public var _PrintEnabled:Boolean = true;//是否支持打印 [Bindable] public var _FullScreenAsMaxWindow:Boolean = false;//是否支付全屏 [Bindable] public var _ProgressiveLoading:Boolean = false;//是否延迟加载 [Bindable] public var _localeChain:String = "zh_CN";//语言 private var isFocus:Boolean = false; //------------------------ [Bindable] public var _SearchMatchAll:Boolean = true; [Bindable] public var _PrintPaperAsBitmap:Boolean = false; [Bindable] public var _ViewModeToolsVisible:Boolean = true; [Bindable] public var _ZoomToolsVisible:Boolean = true; [Bindable] public var _NavToolsVisible:Boolean = true; [Bindable] public var _CursorToolsVisible:Boolean = true; [Bindable] public var _SearchToolsVisible:Boolean = true; //初始化参数 private function initApp():void{ var params:Object = Application.application.parameters; _Scale = getNumber(params, "Scale", 1); _SwfFile = getString(params, "SwfFile", "Paper.swf"); _EncodeURI = getNumber(params, "EncodeURI", 1); _ZoomTransition = getString(params, "ZoomTransition", "easeOut"); _ZoomTime = getNumber(params, "ZoomTime", 0.6); _MinZoomSize = getNumber(params, "MinZoomSize", 0.2); _MaxZoomSize = getNumber(params, "MaxZoomSize", 5); _ZoomInterval = getNumber(params, "ZoomInterval", 0.1); _FitPageOnLoad = getBoolean(params, "FitPageOnLoad", false); _FitWidthOnLoad = getBoolean(params, "FitWidthOnLoad", false); _PrintEnabled = getBoolean(params, "PrintEnabled", true); _FullScreenAsMaxWindow = getBoolean(params, "FullScreenAsMaxWindow", false); _ProgressiveLoading = getBoolean(params, "ProgressiveLoading", true); _localeChain = params["localeChain"]; _SearchMatchAll=getBoolean(params, "SearchMatchAll", true); _PrintPaperAsBitmap=getBoolean(params, "PrintPaperAsBitmap", false); _ViewModeToolsVisible=getBoolean(params, "ViewModeToolsVisible", true); _ZoomToolsVisible=getBoolean(params, "ZoomToolsVisible", true); _NavToolsVisible=getBoolean(params, "NavToolsVisible", true); _CursorToolsVisible=getBoolean(params, "CursorToolsVisible", true); _SearchToolsVisible=getBoolean(params, "SearchToolsVisible", true); //注册事件监听 this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver); this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut); this.addEventListener(MouseEvent.CLICK,setFocusTo); this.addEventListener("onPageLoaded",pageLoadedEventHandler); //开放给外部(javascript)调用 ExternalInterface.addCallback("hasFocus", hasFocus); ExternalInterface.addCallback("setViewerFocus", setViewerFocus); ExternalInterface.addCallback("gotoPage", gotoPage); ExternalInterface.addCallback("fitWidth", fitWidth); ExternalInterface.addCallback("fitHeight", fitHeight); ExternalInterface.addCallback("loadSwf", loadSwf); ExternalInterface.addCallback("printPaperRange", printPaperRange); ExternalInterface.addCallback("searchText", searchText); ExternalInterface.addCallback("prevSearchMatch", prevSearchMatch); ExternalInterface.addCallback("nextSearchMatch", nextSearchMatch); ExternalInterface.addCallback("switchMode", switchMode); ExternalInterface.addCallback("Zoom", Zoom); ExternalInterface.addCallback("nextPage", nextPage); ExternalInterface.addCallback("getCurrPage", getCurrPage); ExternalInterface.addCallback("prevPage", prevPage); ExternalInterface.addCallback("printPaper", printPaper); ExternalInterface.addCallback("getTotalPages", getTotalPages); } //---------------------------事件------------------------------ protected function onLoadedError(event:ErrorEvent):void{ dispatchEvent(event); } private function setFocusTo(e:MouseEvent):void{ stage.stageFocusRect = false; stage.focus = e.target as InteractiveObject; } private function onMouseOver(event:MouseEvent):void{ this.isFocus = true; } private function onMouseOut(event:MouseEvent):void{ this.isFocus = false; } protected function onCurrPageChanged(event:CurrentPageChangedEvent):void{ dispatchEvent(event); } protected function documentLoadedErrorHandler(event:Event):void{ dispatchEvent(event); } protected function onExternalLinkClickedHandler(event:ExternalLinkClickedEvent):void{ dispatchEvent(event); } protected function onDocumentLoadingProgressHandler(event:ProgressEvent):void{ dispatchEvent(event); } protected function papersLoadingHandler(event:Event):void{ dispatchEvent(event); } protected function pageLoadingEventHandler(event:Event):void{ dispatchEvent(event); } protected function documentPrintedHandler(event:DocumentPrintedEvent):void{ dispatchEvent(event); } protected function documentLoadedHandler(event:Event):void{ dispatchEvent(event); } protected function pageLoadedEventHandler(event:Event):void{ dispatchEvent(event); } protected function errorLoadingPageEventHandler(event:ErrorLoadingPageEvent):void{ dispatchEvent(event); } //----------------------------函数----------------------------- public function getTotalPages():Number{ return FlexPaper.getTotalPages(); } public function printPaper(e:Event):void{ FlexPaper.printPaper(e); } public function printPaperRange(range:String):void{ FlexPaper.printPaperRange(range); } public function searchText(text:String):void{ FlexPaper.searchText(text); } public function prevSearchMatch():void{ FlexPaper.prevSearchMatch(); } public function nextSearchMatch(text:String):void{ FlexPaper.nextSearchMatch(text); } public function switchMode(mode:String=null):void{ FlexPaper.switchMode(mode); } public function Zoom(factor:Number):void{ FlexPaper.Zoom(factor); } public function nextPage():void{ FlexPaper.nextPage(); } public function prevPage():void{ return FlexPaper.prevPage(); } public function getCurrPage():Number{ return FlexPaper.getCurrPage(); } public dynamic function loadSwf(s:String):void{ FlexPaper.SwfFile = s; } public function fitHeight():void{ FlexPaper.fitHeight(); } public function fitWidth():void{ FlexPaper.fitWidth(); } public function gotoPage(p:Number):void{ FlexPaper.gotoPage(p); } public function hasFocus():Boolean{ return isFocus; } public function setViewerFocus(isFocus:Boolean):void{ this.FlexPaper.setViewerFocus(); } /** * * 获取String类型参数 * 如果没有,则返回默认值 **/ private function getString(params:Object, name:String, def:String):String{ if(params[name] != null){ return params[name]; } return def; } private function getNumber(params:Object, name:String, def:Number):Number{ if(params[name] != null){ return params[name]; } return def; } private function getBoolean(params:Object, name:String, def:Boolean):Boolean{ if(params[name] != null){ return params[name] == "true"; } return def; } ]]> </mx:Script> <fp:FlexPaperViewer id="FlexPaper" width="100%" height="100%" onDocumentLoaded="documentLoadedHandler(event)" onDocumentLoading="papersLoadingHandler(event)" onDocumentLoadedError="documentLoadedErrorHandler(event)" onLoadingProgress="onDocumentLoadingProgressHandler(event)" onCurrPageChanged="onCurrPageChanged(event)" onExternalLinkClicked="onExternalLinkClickedHandler(event)" onDocumentPrinted="documentPrintedHandler(event)" onPageLoaded="pageLoadedEventHandler(event)" onPageLoading="pageLoadingEventHandler(event)" onErrorLoadingPage="errorLoadingPageEventHandler(event)" Scale="{_Scale}" SwfFile="{_SwfFile}" ZoomTransition="{_ZoomTransition}" ZoomTime="{_ZoomTime}" MinZoomSize="{_MinZoomSize}" MaxZoomSize="{_MaxZoomSize}" ZoomInterval="{_ZoomInterval}" FitPageOnLoad="{_FitPageOnLoad}" FitWidthOnLoad="{_FitWidthOnLoad}" EncodeURI="{_EncodeURI}" FullScreenAsMaxWindow="{_FullScreenAsMaxWindow}" ProgressiveLoading="{_ProgressiveLoading}" SearchMatchAll="{_SearchMatchAll}" PrintPaperAsBitmap="{_PrintPaperAsBitmap}" ViewModeToolsVisible="{_ViewModeToolsVisible}" ZoomToolsVisible="{_ZoomToolsVisible}" NavToolsVisible="{_NavToolsVisible}" CursorToolsVisible="{_CursorToolsVisible}" SearchToolsVisible="{_SearchToolsVisible}" locale="{_localeChain}" PrintEnabled="{_PrintEnabled}" /> </mx:Application>
注:如果上面代码出现<p></p>标签请删除,这是博客自动加上去的。
设置该项目属性,将附加的编译参数修改成如下所示:
-locale=en_US,fr_FR,zh_CN,es_ES,ru_RU,pt_BR,hu_HU,tr_TR,se_SE,pt_PT,el_EL,dn_DN,cz_CS,it_IT,de_DE,pl_PL,fi_FN,pv_FN,nl_NL,bg_BG -source-path=locale/{locale}
如果语言切换只涉及中英文的话,只需要:-locale=en_US,zh_CN -source-path=locale/{locale} 即可。
主要总结:
1、mark文中没有添加多余可用的api接口,从Diyflexpaper.mxml代码中可以看到,已经添加的函数有:
//开放给外部(javascript)调用 ExternalInterface.addCallback("hasFocus", hasFocus); ExternalInterface.addCallback("setViewerFocus", setViewerFocus); ExternalInterface.addCallback("gotoPage", gotoPage); ExternalInterface.addCallback("fitWidth", fitWidth); ExternalInterface.addCallback("fitHeight", fitHeight); ExternalInterface.addCallback("loadSwf", loadSwf); ExternalInterface.addCallback("printPaperRange", printPaperRange); ExternalInterface.addCallback("searchText", searchText); ExternalInterface.addCallback("prevSearchMatch", prevSearchMatch); ExternalInterface.addCallback("nextSearchMatch", nextSearchMatch); ExternalInterface.addCallback("switchMode", switchMode); ExternalInterface.addCallback("Zoom", Zoom); ExternalInterface.addCallback("nextPage", nextPage); ExternalInterface.addCallback("getCurrPage", getCurrPage); ExternalInterface.addCallback("prevPage", prevPage); ExternalInterface.addCallback("printPaper", printPaper); ExternalInterface.addCallback("getTotalPages", getTotalPages);
2、解决中文名swf无法加载问题
首先我是按mark的方法修改,但还是不能加载,他的方法是:
SwfFile : escape('中文ajava.swf')改为SwfFile : decodeURI('中文ajava.swf')
Viewer.as - set SwfFile(s:String) 方法中,修改如下代码:
------------------------------------------------------------------- if(EncodeURI) s = unescape(s); 改为 if(EncodeURI) s = decodeURI(s); -------------------------------------------------------------------- if(!pagesSplit){ _swfFile = s; if(EncodeURI) _swfFile = encodeURI(s); else _swfFile = s; } else _swfFile = s; 改为 _swfFile = s;
以他的意思是去除整个if语句,应该是这个意思,修改后还是加载不了,后来发现问题应该只出在前面s=unescape(s)这里,改为s=decodeURI(s),只改这里,后面的if语句保留,而SwfFile : escape('中文ajava.swf')改为SwfFile : encodeURI('中文ajava.swf') ,这里我采用encodeURI,因为encodeURI和decodeURI是配对的,不可能两次使用 decodeURI吧?而且测试发现,使用loadSwf()方法,如果用loadSwf(decodeURI(url))是加载不了的(当然这里说的都是加载中文名的swf),特两处统一用encodeURI()编码。
3、js中如何调用flex中的方法,比如调用loadSwf()函数。
其实flash版的flexpaper的flexpaper_flash.js文件中开头已经定义一个获取flex对象的方法:
window.$FlexPaper = window["$FlexPaper"] = function(){ if (window['flexpaper']) return window['flexpaper']; else window['flexpaper'] = window.FlexPaperViewer_Instance.getApi(); return window['flexpaper']; };那么我们就可以这样用了:
var swf="代码规范.swf"; var pdf=$FlexPaper(); pdf.loadSwf(encodeURI(swf));4、如何分页加载swf文件。
这里引用一下别人的方法:
C:\SWFTools\pdf2swf.exe Paper.pdf -o Paper%.swf-f -T 9 -t -s storeallcharacters
大家可以看到上面的命令行比原来只多了一个%符号,这个符号表示在PDF2SWF转换文档时,将为每一个页创建一个swf文件。如:"Paper1.swf", "Paper2.swf"。然后在FlexPaper控件配置加载SwfFile的地方,采用:{filename[*,padding],total pages}这种语法表明。比如:SwfFile :"{"+encodeURI(url+"热效应保护标准原文")+"[*,1].swf,13}"。
padding是递增数,设置为1;total pages是总页数。
注:当加载分页的swf文件后,调用loadSwf()函数切换swf会出错,我测试是这样的,还未研究解决办法。
很明显这个地址完全是错误的,为什么?
主要探讨问题:
1、Diyflexpaper.mxml中的事件仿照FlexPaperViewer.mxml的方法,但在js中如何触发并处理事件还不会(呵呵)。
2、分页加载的swf切换错误问题待解决。
3、初始化未设置参数InitViewMode,这是因为加入该参数提示错误:
到此,先记下这些,后面继续研究。