看了一下百度文档分享平台的页面,对其中的技术实现,做了一些研究,还好有些成果,就拿来与大家分享一下。
FlexPaper是一个开源的轻量级文档显示组件,被设计用来与PDF2SWF一起使用,使在Flex中显示PDF成为可能。它可以被当做Flex的library来使用。
一. 使用PDF2SWF准备好你的文档
首先要将PDF转成SWF,这步可以使用开源的SwfTools自动完成
1.下载安装 SwfTools
2. 可以通过命令行的方式,例如将Paper3.pdf转换成Paper3.swf
C:SWFToolspdf2swf Paper3.pdf -o Paper3.swf
下载安装后用它的界面软件进行操作不行,不知道为什么,但通过命令行却可以。
三. 在Flex中使用FlexPaper
1. 下载FlexPaper SWC,添加到你的Flex项目libs中
2. 复制你生成的SWF文件到你的bin-debug目录,如Paper3.swf,添加FlexPaper组件到你的flex代码中
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"???
??? layout="absolute"
??? width="800" height="500"
??? xmlns:flexpaper="com.devaldi.controls.flexpaper.*">
??????
??? <flexpaper:FlexPaperViewer width="800" height="500"???
??????? Scale="1" SwfFile="Paper3.swf" />
??????
</mx:Application>
然后运行mxml文件就看到效果了。
源码到csdn上下载:
FlexPaperExample1.zip????????????? 例子程序
FlexPaper_1.0_swc.zip????????????? flexPaper组件
swftools????????????????????????????????????? 到网上自己下
=======================================
这只是对pdf的解决方案,对offic文档我的想法是转换成html.然后再显示,用的是jacob来转换。
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JacobUtil {
public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int EXCEL_HTML = 44;
/**
* WORD转HTML
*
* @param docfile
*??????????? WORD文件全路径
* @param htmlfile
*??????????? 转换后HTML存放路径
*/
public static void wordToHtml(String docfile,String htmlfile) {
?? ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
?? try {
??? app.setProperty("Visible",new Variant(false));
??? Dispatch docs = app.getProperty("Documents").toDispatch();
??? Dispatch doc = Dispatch.invoke(
????? docs,
????? "Open",
????? Dispatch.Method,
????? new Object[] { docfile,new Variant(false),
??????? new Variant(true) },new int[1]).toDispatch();
??? Dispatch.invoke(doc,"SaveAs",Dispatch.Method,new Object[] {
????? htmlfile,new Variant(WORD_HTML) },new int[1]);
??? Variant f = new Variant(false);
??? Dispatch.call(doc,"Close",f);
?? } catch (Exception e) {
??? e.printStackTrace();
?? } finally {
??? app.invoke("Quit",new Variant[] {});
?? }
}
/**
* EXCEL转HTML
*
* @param xlsfile
*??????????? EXCEL文件全路径
* @param htmlfile
*??????????? 转换后HTML存放路径
*/
public static void excelToHtml(String xlsfile,String htmlfile) {
?? ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word
?? try {
??? app.setProperty("Visible",new Variant(false));??? Dispatch excels = app.getProperty("Workbooks").toDispatch();??? Dispatch excel = Dispatch.invoke(????? excels,????? "Open",????? Dispatch.Method,????? new Object[] { xlsfile,??????? new Variant(true) },new int[1]).toDispatch();??? Dispatch.invoke(excel,new Object[] {????? htmlfile,new Variant(EXCEL_HTML) },new int[1]);??? Variant f = new Variant(false);??? Dispatch.call(excel,f);?? } catch (Exception e) {??? e.printStackTrace();?? } finally {??? app.invoke("Quit",new Variant[] {});?? }}}现在还没有测试成功。待续