最近系统需要做在线预览,
有三种方法:
1.转换为pdf,直接页面显示
2.转换为pdf再转换为swf,用FlexPaper控制显示
3.转换为html
,
第一种方法实现如下:
需要
jodconverter-2.2.2.zip中的包下载地址:
http://download.csdn.net/download/qi923701/10174191
需要安装oppenoffice链接地址:http://www.openoffice.org/download/index.html
安装完后启动oppenoffice
启动方法
1:cd C:\Program Files\OpenOffice.org3\program
2:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard
启动后 上代码::首先转换为pdf
测试代码:
public static void main(String[] args) throws ConnectException {
File inputFile = new File("G:/444.docx");
File outputFile = new File("G:/222.pdf");
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
}
测试成功::封装为方法:如下
public static void getPdf(String wordFile,String pdfFile) throws ConnectException{
File inputFile = new File(wordFile);
File outputFile = new File(pdfFile);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);//链接oppenoffice
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
}
controller代码:
@RequestMapping(value = "/readOnline")
public void readOnline(HttpServletRequest request,HttpServletResponse response) throws IOException {
String token = UUID.randomUUID().toString();
Test002.getPdf("G:/444.docx","G:/"+token+".pdf");
File file =new File("G:/"+token+".pdf");
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
"inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) != -1)
out.write(buf, 0, len);
br.close();
out.close();
}
测试成功,,zhong
第二种方法:
将pdf转换为swf:这个需要swftools可以网上自己下载
封装方法如下:
/** * 转换成 swf */
@SuppressWarnings("unused")
private static void pdf2swf(File pdfFile,File swfFile) throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1) {// windows环境处理
try {
//C:/Program Files (x86)/SWFTools/pdf2swf.exe:是swftools安装位置
Process p = r.exec("C:/Program Files (x86)/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println("****swf转换成功,文件输出: "+swfFile.getPath() + "****");
if (pdfFile.exists()){
pdfFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
throw e;
}
} else if (environment == 2) {// linux环境处理
try {
Process p = r.exec("pdf2swf" + pdfFile.getPath()+ " -o " + swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println("****swf转换成功,文件输出: "+ swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
} else {
System.out.println("****pdf不存在,无法转换****");
}
} else {
System.out.println("****swf已经存在不需要转换****");
}
}
static String loadStream(InputStream in) throws IOException {
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ((ptr = in.read()) != -1) {
buffer.append((char) ptr);
}
return buffer.toString();
}
然后
需要FlexPaper引入
<script src="${ctx}/static/FlexPaper_2.3.6/js/jquery.min.js"></script>
<script type="text/javascript" src="${ctx}/static/FlexPaper_2.3.6/js/flexpaper.js"></script>
<script type="text/javascript" src="${ctx}/static/FlexPaper_2.3.6/js/flexpaper_handlers.js"></script>
显示swf的html代码
<div id="documentViewer" class="flexpaper_viewer" style="boder:1px solid red;width:100%;height:1000px">loading document... </div>
<script type="text/javascript">
$('#documentViewer').FlexPaperViewer(
{ config : {
SWFFile : '${ctx}/static/333.swf',
Scale : 1.0,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
RenderingOrder : 'flash',
StartAtPage : '',
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
jsDirectory:'${ctx }/static/FlexPaper_2.3.6/js/',//根据自己项目设置
WMode : 'window',
localeChain: 'en_US'
}}
);
这些参数的意思参考:http://www.codeweblog.com/flexpaper显示swf文件示例和参数设置/
至此,swf转换显示也已经完成,
第三种方法:
直接将word转换为html,之后就可以根据自己的意愿随意改动,封装方法如下
/**
* 将word文档转换成html文档
*
* @param docFile
* 需要转换的word文档
* @param filepath
* 转换之后html的存放路径
* @return 转换之后的html文件
*/
public static File convert(File docFile, String filepath) {
// 创建保存html的文件
File htmlFile = new File(filepath + "/" + new Date().getTime()
+ ".html");
// 创建Openoffice连接
OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);
try {
// 连接
con.connect();
} catch (ConnectException e) {
System.out.println("获取OpenOffice连接失败...");
e.printStackTrace();
}
// 创建转换器
DocumentConverter converter = new OpenOfficeDocumentConverter(con);
// 转换文档问html
converter.convert(docFile, htmlFile);
// 关闭openoffice连接
con.disconnect();
return htmlFile;
}