Java 实现文档在线阅读功能

Java 实现文档在线阅读功能

 

1.实现思路:本文的实现思路是利用先将文档(包括.doc/.docx/.xls等文件类型)转换成pdf格式的文件,再将pdf文件转换成为flash文件,将flash文件在前台页面利用flash播放器进行展示。

 

2.工具:

 (1) operoffice.org  operoffice.org 是一套sun公司开源的office办公套件,能在windowslonuxsolaris等操作系统上运行(可跨平台)。实现此功能需要安装opernoffice.org,在实现此功能时需要启动openoffice的服务(opernoffice.server)

Opernoffice.org 下载地址:

http://dlsw.baidu.com/sw-search-sp/soft/70/15989/Apache_OpenOffice_4.0.1_Win_x86_install_zh-CN.728932882.exe

(2) jodconverter-2.2.2.jar 

 下载 jodconverter-2.2.2 ,将lib目录所有jar 复制到项目 lib目录下载 

(3)swftools-0.9.2.exe

 下载swftools-0.9.2.exe 安装后,复制安装目录中到 pdf2swf.exe 到项目附件目录中。

 

3.实现步骤

 (1) 程序中需要引入的包

     .

     .

     .

import com.artofsolving.jodconverter.DocumentConverter; 

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; 

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; 

import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

 

 

2.将文档转换成swf文件

以一个test.doc文件为例(其他文件一样)

String fileSuffix = "......."; //文件的前缀,即"test"

String fileType = "........."; //文件类型,即"doc"

String fileRealPath = "..........."; //文件的实际路径

String flashFilePath = "......."; //临时存放生成的swf文件路径

String pdfFilePath = "......" //临时存放生成的pdf文件路径

//创建test.doc的swf文件,即test.swf

File flashFile = new File(flashFilePath + fileSuffix + "swf");

//如果test.swf文件不存在

if(!flashFile.exists()){

//1.如果文件为pdf格式,则只需将文件移动到/pdfTemp路径下,不需要openoffice转换了
//2.如果文件为其他格式,则需先将文件的格式转化为pdf格式

File sourceFile = new File(fileRealPath + fileSuffix +"." + fileType);

if(fileType.equals("pdf")){

//如果为pdf文件,则将其复制到临时存放生成的pdf文件路径

sourceFile.renameTo(new File(pdfFilePath, sourceFile.getName()));

}else{

File pdfFile = new File(pdfFilePath + fileSuffix + "." + "pdf");

//使用openoffice的API接口转化为pdf文件JOD4DocToPDF.docToPdf(sourceFile, pdfFile);

}

try {

//将pdf文件转化为swf文件(第一个参数为swf文件的名字,第二个参数为pdf文件的名字,详细可参见下方的代码)
FlashPaper.convertPDF2SWF(fileSuffix, fileSuffix);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public class FlashPaper extends Thread {
public static int convertPDF2SWF(String swfName,String documentName) throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
String pdfPath = request.getSession().getServletContext().getRealPath(pdfFilePath+documentName+".pdf");
String swfPath = request.getSession().getServletContext().getRealPath("swfFilePath+swfName+".swf");
目标路径不存在则建立目标路径
File dest = new File(request.getSession().getServletContext().getRealPath("/swfTemp"));
if (!dest.exists()) dest.mkdirs();

//源文件不存在则返回
File source = new File(pdfPath);
if (!source.exists()) return 0;

//调用pdf2swf命令进行转换(安装swftools路径为 D盘根目录)
String command= "D:/Program Files/SWFTools/pdf2swf.exe -t \""+pdfPath+"\" -o \""+swfPath+"\" -s flashversion=9 -s languagedir=c://xpdftest//xpdf-chinese-simplified";
System.out.println("cmd:"+command);
// Process pro = Runtime.getRuntime().exec(command);
Process process = Runtime.getRuntime().exec(command); // 调用外部程序
final InputStream is1 = process.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(is1));
try {
while(br.readLine()!= null) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
InputStream is2 = process.getErrorStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
StringBuilder buf = new StringBuilder(); // 保存输出结果流
String line = null;
while((line = br2.readLine()) != null) buf.append(line); // 循环等待ffmpeg进程结束
System.out.println("输出结果为:" + buf);

while (br2.readLine() != null);

try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return process.exitValue();


}

}

3.flexpaper组件在前台页面显示swf文件

(项目中导入flexpaper文件,在显示页面指定路径即可显示)

<script type="text/javascript"> 

var fp = new FlexPaperViewer( 

'hlbussiness/planbaseViewer/FlexPaperViewer', 

'viewerPlaceHolder', { config : { 

SwfFile : escape("${swfPath}"),<span style="white-space: pre;"> </span>//swf文件路径 

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', 

PrintPaperAsBitmap : false, 

ViewModeToolsVisible : true, 

ZoomToolsVisible : true, 

NavToolsVisible : true, 

CursorToolsVisible : true, 

SearchToolsVisible : true, 

localeChain: 'zh_CN' 

}}); 

</script> 

 

 注意:swftools是收费软件,对于商用产品要谨慎使用。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值