要实现word、ppt、excel、pdf等文档在web应用端预览功能,目前一般做法为:
  在页面的显示效果:
主要用的工具:
  Openoffice4(windows、linux);SWFTools(windows、linux);Flexpaper插件(flexpaper_flash.js);
首先在电脑端安装好openoffice与swftools软件,记住他们的安装位置(以windows下使用为例),安装位置路径最好不能有空格,最新版的openoffice对于各类office文档支持都较好。Java端使用openoffice需要几个jar包,可以通过maven下载或者直接把jar包引入项目:
jodconverter-core-3.0-alfresco-patched-20141024.jar(此jar包maven下载不了)
其他maven依赖:
<dependency><groupId>org.openoffice</groupId><artifactId>ridl</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>unoil</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>jurt</artifactId><version>4.1.2</version></dependency>
<dependency><groupId>org.openoffice</groupId><artifactId>juh</artifactId><version>4.1.2</version>
</dependency>
Java测试类:
 
package com.cdv.webview;import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Pattern;import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.junit.Test;public class DocConverter {@Testpublic void test() {
    conver();}String filePath = "d:\\Users\\Administrator\\Desktop\\软件包\\symantec公开报价.xlsx";String fileName = filePath.substring(0, filePath.lastIndexOf("."));String outputPath = "f:\\";int environment = 1;// 环境 1:Windows 2:LinuxFile pdfFile = new File(fileName+ ".pdf");File swfFile = new File(fileName+ ".swf");File docFile = new File(filePath);/**  * @Title: doc2pdf  * @Description: 文档转换为pdf文件* @throws Exception*/private void doc2pdf() throws Exception {DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();  String officeHome = getOfficeHome();  config.setOfficeHome(officeHome);  OfficeManager officeManager = config.buildOfficeManager();  officeManager.start();  OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  if (docFile.exists()) {// 找不到源文件, 则返回  if (!pdfFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径  pdfFile.getParentFile().mkdirs();  }  converter.convert(docFile, pdfFile);  }  officeManager.stop();  }/**  * @Title: getOfficeHome  * @Description: 根据不同系统获取openoffice安装路径* @return String*/public static String getOfficeHome() {  String osName = System.getProperty("os.name");  if (Pattern.matches("Linux.*", osName)) {  return "/opt/openoffice.org3";  } else if (Pattern.matches("Windows.*", osName)) {  return "C:\\Program Files (x86)\\OpenOffice 4";  } else if (Pattern.matches("Mac.*", osName)) {  return "/Application/OpenOffice.org.app/Contents";  }  return null;  }  /**  * @Title: pdf2swf  * @Description: pdf文件转换为swf文件* @throws Exception*/private void pdf2swf() throws Exception {Runtime r = Runtime.getRuntime();if (!swfFile.exists()) {if (pdfFile.exists()) {if (environment == 1) {// windows环境处理try {Process p = r.exec("d:\\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已经存在不需要转换****");}}/**  * @Title: loadStream  * @Description: 获取输出结果* @param in* @return String* @throws IOException*/public 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();}/**  * @Title: conver  * @Description: 文档转换为swf方法* @return boolean*/public boolean conver() {if (swfFile.exists()) {System.out.println("****swf转换器开始工作,该文件已经转换为 swf****");return true;}if (environment == 1) {System.out.println("****swf转换器开始工作,当前设置运行环境 windows****");} else {System.out.println("****swf转换器开始工作,当前设置运行环境 linux****");}try {doc2pdf();pdf2swf();} catch (Exception e) {e.printStackTrace();return false;}if (swfFile.exists()) {System.out.println("****转换成功****");return true;} else {System.out.println("****文件不存在,转换失败****");return false;}}
}
Jsp页面使用flexpaper_flash.js,主要的包结构,symantec.swf和test.swf为生成的swf测试文件,其他为flexpaper需要的文件。
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String swfFilePath=new String("resources/test.swf");System.out.println("展示路径:"+swfFilePath);%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="js/flexpaper_flash.js"></script>
<style type="text/css" media="screen">
html,body{height: 100%;
}
body{margin: 0;padding: 0;overflow: auto;
}
#flashContent{display: none;
}
</style>
<title>在线文档预览</title>
</head>
<body><div style="position: absolute; left:50px;top:10px;"><a id="viewerPlaceHolder" style="width: 820px;height: 650px;display: block;"></a><script type="text/javascript">var fp=new FlexPaperViewer('FlexPaperViewer','viewerPlaceHolder',{config:{SwfFile:encodeURI('<%=swfFilePath%>'),Scale:1.2,ZoomTransition:'easeOut',ZoomTime:0.5,ZoomInterval:0.2,FitPageOnLoad:false,FitWidthOnload:false,FullScreenAsMaxWindow:false,ProgressiveLoading:false,MinZoomSize:0.2,MaxZoomSize:5,SearchMatchAll:false,InitViewMode:'SinglePage',RenderingOrder : 'flash',ViewModeToolsVisible:true,ZoomToolsVisible:true,NavToolsVisible:true,CursorToolsVisible:true,SearchToolsVisible:true,localeChain:'zh_CN'}});</script></div>
</body>
</html>
启动tomcat可以查看此jsp得到预览效果。
所有用到的jar包、js下载地址请点击。

文档文件等网页端预览功能相关推荐

  1. MFC 基于多文档的打印和打印预览功能的实现

    一.基础知识 1 网上有很多的关于打印的程序,一定要看清楚,是基于对话框dialog的打印功能,还是基于文档的打印功能. 如果分不清基于对话框和文档的区别,建议新建一个单文档.多文档和对话框的工程,看 ...

  2. 【玩转云函数】腾讯云云函数结合金山文档打造轻量级 Office 在线预览服务

    以下内容来自「玩转腾讯云」用户原创文章,已获得授权. 本文介绍下如何使用云函数来实现 Office 办公文件的预览 01. 前言 曾几何时,文档预览曾经很麻烦,小公司需要购买服务器,自行搭建文件服务器 ...

  3. html浏览pdf文件,HTML网页在线预览PDF文件

    image.png image.png 今日工作需要,需要在线预览PDF文件,so,做了个研究,下面将网页在线预览pdf的方法做个梳理,大家共同学习成长吧 方法分为三种 1. 方法1-embed标签 ...

  4. Office文件转化PDF实现预览功能

    OpenOffice文件转PDF 实测有用 OpenOffice办公文件预览功能 OpenOffice操作 aspose-words来实现预览 预览操作 OpenOffice办公文件预览功能 Hell ...

  5. 文档转成html在线预览,java poi Word文档转为HTML文件 实现在线预览功能

    Java代码 import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; im ...

  6. FlexPaper控件实现文档的上传和预览

    引言 之前在一个项目中初识了这个控件,当时自己对这个东西非常高的好奇就尝试着做了一个Demo,最近在项目中 遇到了这个需求,所以我向组长推荐了我的这中做法,在之前的系统中是将文档转换成html然后在前 ...

  7. SpringBoot实现服务器PDF文件的下载和预览功能

  8. android打开预览文件格式,Android中文档预览功能的实现思路及问题

    Andriod中的文档在线查看功能,类似于网易邮箱大师中的附件预览功能,要求在app内直接打开office文档.pdf文档等. 思路一:后台统一转换文档格式,安卓端只预览一种格式文档. 在后台将off ...

  9. 智能媒体管理产品文档转换/预览功能介绍(1)---Cloud Native架构

    一.导语 办公文档是每个人日常频繁使用的工具,ppt.word.xls.wps.pdf等为我们工作和生活带来了很多的便利,本文介绍在云计算和智能手机时代,基于 智能媒体管理 的新型Cloud Nati ...

最新文章

  1. Linux C : GDB调试命令汇总
  2. 空间数据引擎oracle_GIS 与Oracle 数据库空间数据格式的转换
  3. 笨方法python3_“笨方法”学Python3,习题 34 。
  4. datagridview绑定数据源不显示_sharding-jdbc系列之 数据源配置(一)
  5. 纯CSS实现圆角边框
  6. hihocoder第196周
  7. CV学习笔记-数字图像概述
  8. 中文字体的英文名称(宋体 微软雅黑)
  9. ios+测试版软件,ios15测试版描述文件
  10. 告别乐盲,AI 通过歌词生成旋律【智能快讯】
  11. 脑肿瘤分割论文打卡2:E1D3 U-Net for Brain Tumor Segmentation
  12. 做网络安全居然不了解ATT&CK?这篇文章的介绍详细到令人尖叫
  13. 如何用Camtasia制作简单动画?
  14. 层次softmax (hierarchical softmax)理解
  15. 趋势跟踪系统的形成历程
  16. 想不想修真鸿蒙源液有什么用,想不想修真初代小世界怎么玩_想不想修真初代小世界玩法介绍_玩游戏网...
  17. swd只能下载一次第二次出现错误
  18. python数据导入与清洗_Python学习之 数据清洗之增删改查
  19. Java itext实现图片转pdf
  20. 电子器件系列十四:缓冲器

热门文章

  1. 一文玩转 WebDriver API
  2. 米赛尔java火_山地自行车推荐 这几款山地自行车值得入手
  3. JavaScript基础知识笔记
  4. Committer identity unknown *** Please tell me who you are...
  5. (Java实习生)每日10道面试题打卡——Java基础知识篇
  6. C++笔记:输入输出、变量、变量加减乘除
  7. Linux服务器域名配置
  8. Chrome 超强生产力工具 Omni
  9. 系统安全 --------- 账号安全管理
  10. 利用python爬虫技术动态爬取地理空间数据云中的元数据(selenium)