1.下载jacob,其中dll文件放到system32目录下,把jacob.jar放到项目lib包下;
2.下载安装SWFTools;下载FlexPaper,把文件夹放到webroot根目录下,把FlexPaper文件夹下的FlexPaperViewer.swf也拷贝到webroot目录下;
3.公共类
package com.venustech.cnnvd.util;
import java.io.File;
import java.io.IOException;
import com.venustech.cnnvd.util.UrlAddress;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
public class ConvertWord {
// pdf 格式
private static final int wdFormatPDF = 17;
// swftools 安装位置
private static String PDF2SWF_PATH = UrlAddress.getUrl("swftoolsinstallpath");
public static void convertWORDtoPDF(String wordFullPath){
String pdfFullPath = String.format("%s.pdf", wordFullPath);
File wordFile = new File(wordFullPath);
File pdfFile = new File(pdfFullPath);
if(!wordFullPath.endsWith(".doc") && !wordFullPath.endsWith(".docx")) {
return;
}
if(!wordFile.exists() && pdfFile.exists()){
return ;
}
// jacob 方式 打开word应用程序
ActiveXComponent app = new ActiveXComponent("Word.Application");
//设置word不可见
app.setProperty("Visible", false);
//获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
//调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatch doc = Dispatch.call(docs,
"Open",
wordFullPath,
false,
true
).toDispatch();
//调用Document对象的ExportAsFixedFormat方法,将文档保存为pdf格式
Dispatch.call(doc,
"ExportAsFixedFormat",
pdfFullPath,
wdFormatPDF //word保存为pdf格式宏,值为17
);
//关闭文档
Dispatch.call(doc, "Close",false);
//关闭word应用程序
app.invoke("Quit", 0);
}
public static void convertPDFtoSWF(String wordFullPath) {
String pdfFullPath = String.format("%s.pdf", wordFullPath);
String swfFullPath = String.format("%s.swf", wordFullPath);
File pdfFile = new File(pdfFullPath);
File swfFile = new File(swfFullPath);
if(!pdfFullPath.endsWith(".pdf") && !pdfFile.exists() && swfFile.exists()){
return ;
}
String command = PDF2SWF_PATH +" \""+pdfFullPath+"\" -o \""+swfFile+"\" -T 9 -f";
try {
// 执行pdf转换swf
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
}
转换类的使用:
// word转换为pdf
ConvertWord.convertWORDtoPDF(penenetKeyfrailty_filepath);
// pdf转换为swf
ConvertWord.convertPDFtoSWF(penenetKeyfrailty_filepath);
这一步可以在提交保存word的时候进行,也可以在预览的时候进行,pdf转换为swf时如果word文件过大,会比较慢,影响用户体验,注意这一点;
5,action
/**
* 在线预览
* @return
*/
public String preview() throws UnsupportedEncodingException {
String wordName = sRequest.getParameter("wordName");
request.put("wordName", wordName);
return SUCCESS;
}
/**
* 预览swf文件加载
* @return
* @throws UnsupportedEncodingException
*/
public String previewSwf() throws UnsupportedEncodingException {
try {
String penenetKeyfrailty_filepath = UrlAddress.getUrl("penenetKeyfrailty_filepath");
String wordName = sRequest.getParameter("wordName");
penenetKeyfrailty_filepath = penenetKeyfrailty_filepath + wordName;
String swfPath = penenetKeyfrailty_filepath + ".swf";
File swfFile = new File(swfPath);
if (swfFile.exists()){
this.inputStream = new FileInputStream(new File(swfPath));
//文件名需要单独转码处理,否则若包含中文则乱码
this.fileName = new String((wordName+".swf").getBytes(), "ISO8859-1");
} else {
request.put(MESSAGE, "文件预览失败!");
return ERROR;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
request.put(MESSAGE, "文件读取失败!!");
return ERROR;
}
return SUCCESS;
}
6. jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>预览</title>
<link href="style/layout.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="FlexPaper/js/jquery.min.js"></script>
<script type="text/javascript" src="FlexPaper/js/flexpaper.js"></script>
<script type="text/javascript" src="FlexPaper/js/flexpaper_handlers.js"></script>
</head>
<body>
<div class="contentcontainer">
<input type="hidden" name="pageNo" id="pageNo" value="<s:if test='%{pageNo != null}'><s:property value='pageNo'/></s:if><s:else>1</s:else>" />
<input type="hidden" id="ids" name="ids" value=""/>
<input type="hidden" id="id" name="id"/>
<s:token></s:token>
<div class="headings altheading">
<h2><s:property value='#request.wordName'/>预览</h2>
</div>
<div class="contentbox">
<div id="documentViewer" style="left:10px;top:10px;height:600px;"></div>
<script type="text/javascript">
var startDocument = "Paper";
$('#documentViewer').FlexPaperViewer(
{ config : {
SWFFile : "xxxx/previewSwfxxxxx.action?wordName=<s:property value='#request.wordName'/>",
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : true,
FullScreenAsMaxWindow : true,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
RenderingOrder : 'flash',
StartAtPage : '',
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
WMode : 'window',
localeChain: 'en_US'
}}
);
</script>
</div>
<br><br>
<p> *首次预览需要转码,等待时间过长,可先进行其他操作,稍后继续预览。</p>
</div>
<script type="text/javascript">
var url = window.location.href.toString();
if(location.length==0){
url = document.URL.toString();
}
if(url.indexOf("file:")>=0){
jQuery('#documentViewer').html("<div style='position:relative;background-color:#ffffff;width:420px;font-family:Verdana;font-size:10pt;left:22%;top:20%;padding: 10px 10px 10px 10px;border-style:solid;border-width:5px;'><img src='http://flexpaper.devaldi.com/resources/warning_icon.gif'> <b>You are trying to use FlexPaper from a local directory.</b><br/><br/> FlexPaper needs to be copied to a web server before the viewer can display its document properlty.<br/><br/>Please copy the FlexPaper files to a web server and access the viewer through a http:// url.</div>");
}
</script>
</body>
</html>