SWF文字查询及高亮显示——第二步:实现文字查询高亮显示基本思路篇

前提:我使用PDF2SWF工具(http://www.swftools.org/)将PDF文档预先转成SWF(优点:1.安全性容易控制,不允许随便下载复制 2浏览方便,不要Adobe插件,客户端普遍装有Flash插件),需要在SWF中实现关键字查询和高亮显示。

基本思路:使用包flash.text中TextSnapshot类,TextSnapshot 对象可用于处理影片剪辑中的静态文本。用到方法:

findText(beginIndex:int, textToFind:String, caseSensitive:Boolean):int
搜索指定的 TextSnapshot 对象,并返回在 beginIndex 位置或其后找到的 textToFind 的第一个匹配项的位置。
setSelected(beginIndex:int, endIndex:int, select:Boolean):void
指定 TextSnapshot 对象中要选择或取消选择的字符范围。
setSelectColor(hexColor:uint = 0xFFFF00):void
指定当突出显示使用 setSelected() 方法选择的字符时要使用的颜色。
 
基本步骤:(转载自:How do I highlight text in the SWF?  http://wiki.swftools.org/index.php/How_do_I_highlight_text_in_the_SWF%3F

Firstly, you must ensure that fonts are embedded within your SWF. To do this, the "-f" and "-F" switches are necessary:

pdf2swf -F $YOUR_FONTS_DIR$ -f input.pdf -o output.swf

This will produce a file where all available fonts from your fonts folder are embedded into the SWF.

Next, you'll need to write up some code to do the search itself. Here is a sample:

private var snapText:TextSnapshot;
            
private function doSearch(event:Event):void {
	if (snapText == null)
		snapText = activeDoc.textSnapshot;
			
	if (searchTI.text != '' && searchTI.text.length > 1) {
		var textPos:int = snapText.findText(0, searchTI.text, false);
		
		snapText.setSelected( 0, snapText.charCount, false );
		
		if (textPos > 0) {
			do {
				snapText.setSelectColor( 0xFFEF00 );
				snapText.setSelected( textPos, textPos + searchTI.text.length, true );
				
				textPos = snapText.findText(textPos + searchTI.text.length, searchTI.text, false);
			}
			while (textPos > 0)
		}
		else {
			Alert.show( "Not found.", "Information" );         		
		}
	}
	else {
		snapText.setSelected( 0, snapText.charCount, false );	            	
	}
}
注:$YOUR_FONTS_DIR$ :为存放PDF文件中用到的字体文件的文件夹,我直接用C:\WINDOWS\Fonts,当然这是我试验阶段用的,应该真的PDF文档中可能用到的字体就行,Windows自带的字体太多了,会使PDF2SWF转换速度慢,转换出的SWF文件变大。使用时建议只用PDF文档中用到的几个字体文件!

 activeDoc 为你加载进去的PDF的SWF后转为MovieClip对象。

原文地址:https://www.cnblogs.com/wuhenke/p/1603776.html