Office文档的在线阅读,现在的一般实现思路如下:
1、使用openoffice将office文档转换成PDF
2、使用swftools将PDF转换成swf
3、使用flexpaper播放swf文件
office文档转PDF
需要的软件及类库
需要软件openoffice 3.4.1,可以从openoffice官网下载。
maven的信息如下:其中version.jodconverter=1.0.5
openoffice的类库似乎是不用引入,代码里面是启动了外部的转换服务
<!-- jodconverter -->
<dependency>
<groupId>com.github.livesense</groupId>
<artifactId>jodconverter-core</artifactId>
<version>${version.jodconverter}</version>
</dependency>
<!-- openoffice
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>${version.openoffice}</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>${version.openoffice}</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>${version.openoffice}</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>${version.openoffice}</version>
</dependency>
-->
第一步:安装openoffice到操作系统里面
第二步:复制相关的jar包到系统的依赖库里面。具体的jar包见上面的代码。
第三步:编写转换代码,核心代码如下:
/**
* 将office文档转换成PDF文档
* @Date 2012-12-11上午10:01:59
* @Author huqiwen
* @param inputFile 文件在磁盘上的物理路径
* @param pdfFile 目标文件在磁盘上的物理路径
* @return
*/
public static File convert2PDF(String inputFile, String pdfFile) {
if (StringUtils.isEmpty(inputFile)||StringUtils.isEmpty(pdfFile)) {
return null;
}
startService();
logger.info("进行文档转换转换:" + inputFile + " --> " + pdfFile);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File targerpdfFile = new File(pdfFile);
converter.convert(new File(inputFile), targerpdfFile);
stopService();
return targerpdfFile;
}
public static void startService() {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
configuration.setOfficeHome(openoffice_home);// 设置OpenOffice.org安装目录
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时
officeManager = configuration.buildOfficeManager();
officeManager.start(); // 启动服务
logger.info("office转换服务启动成功……");
} catch (Exception ce) {
logger.info("office转换服务启动失败!详细信息");
}
}
public static void stopService() {
logger.info("关闭office转换服务....");
if (officeManager != null) {
officeManager.stop();
}
logger.info("关闭office转换成功!");
}
PDF转SWF
上面的过程是完成了office文档到PDF的转换,要想在flexpaper里面播放,还需要将PDF转换成SWF格式的,SWFTOOS是一个工具可以将pdf、图片等格式的内容转换成SWF格式,这里我们主要使用的PDF转换功能。
swftools的官方网站:http://www.swftools.org/
核心内代码如下:
/**
* 把PDF文件转换为SWF的格式,虽然SWFTOOS支持多种格式,但这里主要转换PDF文件
* @Date 2012-12-11上午11:07:14
* @Author huqiwen
* @param sourceFilePath
* @param swfFilePath
* @param fileType
* @return
* @throws IOException
*/
public static boolean convertPDFToSwf(String sourceFilePath,String swfFilePath,String fileType){
if(!swfToolExist()){
logger.warn("未指定要进行swf转化工具的地址!!!");
return false;
}
//判读要转换的文件类型是否符合转换为pdf
if(!Constants.PDF_FILE.equalsIgnoreCase(fileType)){
logger.warn("当前文件不符合要转化为SWF的文件类型!!!");
return false;
}
File sourceFile=new File(sourceFilePath);
if(!sourceFile.exists()){
logger.warn("要进行转换文件不存在!!!");
return false;
}
//检查flash文件的目录是否存在,不存在则创建
File swfFile=new File(swfFilePath);
if (!FileUtil.mkdirs(swfFile)) {
logger.error("目录创建失败[" + swfFile.getPath() + "]");
return false;
}
try {
if(!swftoolsPath.endsWith(File.separator)){
swftoolsPath+=File.separator;
}
List<String> commandBuidler = new ArrayList<String>();
//加载系统的字体目录
String systemFontsDir = properties.getProperty("file.fonts.dir", "C:\\WINDOWS\\Fonts");
if (StringUtils.isNotEmpty(systemFontsDir) && Constants.PDF_FILE.equals(fileType)) {
commandBuidler.add(swftoolsPath + fileType+"2swf.exe");
commandBuidler.add("-F");
commandBuidler.add(systemFontsDir);
commandBuidler.add("-f");
commandBuidler.add(sourceFilePath);
commandBuidler.add("-o");
commandBuidler.add(swfFilePath);
commandBuidler.add("-T 9 ");
if (sourceFile.length() > 1024*1024*10) { //大于10M加上以下命令
commandBuidler.add(" -s poly2bitmap ");
}
} else {
logger.warn("只提供PDF文档的格式转换");
return false;
}
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(commandBuidler);
Process process = processBuilder.start();
AbsInputStreamWathThread inputWathThread = new InputStreamWathThread(process);
inputWathThread.start();
AbsInputStreamWathThread errorInputWathThread = new ErrorInputStreamWathThread(process);
errorInputWathThread.start();
process.waitFor();// 等待子进程的结束,子进程就是系统调用文件转换这个新进程
inputWathThread.setOver(true); // 转换完,停止流的处理
errorInputWathThread.setOver(true);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
使用flexpaper播放
flexpaper的官方网站:http://flexpaper.devaldi.com/
从网站上下载免费版本即可,http://flexpaper.devaldi.com/download/
1、在页面中加载flexpaper.js和flexpaper_handlers.js两个JS文件
2、在页面中要显示flexpaper的地方使用以下JS:
下面的jsDirectory的目录虽然叫js目录,但这个目录的目的主要是定位FlexPaperViewer.swf的位置的,比如现在jsDirectory : "${basepath}/resources/js/", ,则将 FlexPaperViewer.swf放在${basepath}/resources/下面
$('#nodeId').FlexPaperViewer(
{ config:{
SWFFile : swfurl,
jsDirectory : "${basepath}/resources/js/",
Scale : 0.9,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
FitPageOnLoad : true, //加载后适合高度
FitWidthOnLoad : true,//加载后适合宽度
FullScreenAsMaxWindow : false, //是否支持全屏
ProgressiveLoading : false, //是否支持延迟加载
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'zh_CN'
}}
);
参考
http://www.huqiwen.com/2013/01/10/document-online-preview/