java 预览word文档_Java实现office文档与pdf文档的在线预览功能

本文介绍了一个使用Java实现Office文档(如Word、Excel、PowerPoint)与PDF在线预览的功能。通过OpenOffice将Office转换为PDF,再利用Swftools将PDF转换为SWF文件,最后使用FlexPaperViewer插件在浏览器中展示SWF实现预览。详细介绍了转换和预览的步骤,包括所需软件的安装和配置,以及Java代码实现。
摘要由CSDN通过智能技术生成

最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完。压力略大。后面查找百度资料、以及在同事与网友的帮助下,四天多把它做完。查找资料发现我们要实现的过程就是把office转换成pdf,当然pdf就不用转换了。然后在pdf转换为swf文件,在浏览器实现预览swf文件。整个过程就是这样,看起来很简单,实际操作起来会出现各种问题。下面我就把自己写的这一小功能记录下来。

1、首先我们需要找到可以把office转换成pdf的方法,查找资料发现有openoffice这一软件可以把office转换成pdf,这一软件先下载下来,然后记住自己安装的在那个位置。然后在cmd环境下进入安装目录的program目录,输入打开openoffice的命令:

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

输入完成之后在任务管理器可以看见soffice.bin的进程在任务管理器,这一服务就启动成功。当然在代码中转换office2pdf我们还需要一些架包。下载jodconverter-2.2.2架包,然后复制到lib目录下,引入架包就可以了。这个架包有如下包:

a2046e7e5c6deb575ad59f51f6a60275.png

有一些项目重复的可以删除,根据实际情况自己处理。

2、我们需要找到转换pdf2swf的方法。查找资料发现swftools这个软件可以把pdf转换成swf文件。把它下下来安装好就可以了。

3、我们需要一个展示swf文件的容器,发现有flexpaper这个插件。而且展示效果还不错。所以我们需要下载这个插件。使用这个插件需要有三个js文件。分别是:jquery.js、flexpaper_flash.js、flexpaper_flash_debug.js。插件的名字是FlexPaperViewer.swf。

整个项目结如下:

2de054818ffcec766ea0706b372f7c20.png

准备工作完成下面开始编码.

转换类为DocConverter的代码:

package com.cectsims.util;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

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;

/**

* doc docx格式转换

*/

public class DocConverter {

private static final int environment = 1;// 环境 1:Windows 2:Linux

private String fileString;// (只涉及PDF2swf路径问题)

private String outputPath = "";// 输入路径 ,如果不设置就输出在默认 的位置

private String fileName;

private File pdfFile;

private File swfFile;

private File docFile;

public DocConverter(String fileString) {

ini(fileString);

System.out.println("文件路径"+fileString);

}

/**

* * 重新设置file

*

* @param fileString

* 32.

*/

public void setFile(String fileString) {

ini(fileString);

}

/**

* * 初始化

*

* @param fileString

*

*/

private void ini(String fileString) {

this.fileString = fileString;

fileName = fileString.substring(0, fileString.lastIndexOf("."));

docFile = new File(fileString);

pdfFile = new File(fileName+ ".pdf");

swfFile = new File(fileName+ ".swf");

}

/**

* 转为PDF

*

* @param file

*

*/

private void doc2pdf() throws Exception {

if (docFile.exists()) {

if (!pdfFile.exists()) {

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

try {

connection.connect();

DocumentConverter converter = new OpenOfficeDocumentConverter(

connection);

converter.convert(docFile, pdfFile);

// close the connection

connection.disconnect();

System.out.println("****pdf转换成功,PDF输出: "+ pdfFile.getPath() + "****");

} catch (java.net.ConnectException e) {

e.printStackTrace();

System.out.println("****swf转换器异常,openoffice 服务未启动!****");

throw e;

} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {

e.printStackTrace();

System.out.println("****swf转换器异常,读取转换文件 失败****");

throw e;

} catch (Exception e) {

e.printStackTrace();

throw e;

}

} else {

System.out.println("****已经转换为pdf,不需要再进行转化 ****");

}

} else {

System.out.println("****swf转换器异常,需要转换的文档不存在, 无法转换****");

}

}

/** * 转换成 swf */

@SuppressWarnings("unused")

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:/Program/swfttools/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();

}

/**

* * 转换主方法

*/

@SuppressWarnings("unused")

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;

}

System.out.println("文件存在吗?"+swfFile);

if (swfFile.exists()) {

System.out.println("存在");

return true;

} else {

System.out.println("不存在");

return false;

}

}

/**

*返回文件路径

* @param

*/

public String getswfPath(){

if (this.swfFile.exists()){

String tempString = swfFile.getPath();

tempString = tempString.replaceAll("\\\\", "/");

System.out.println("最后文件路径为"+tempString);

return tempString;

} else {

return "文件不存在";

}

}

/**

* 设置输出路径

*

* @param outputPath

*/

public void setOutputPath(String outputPath){

this.outputPath = outputPath;

if (!outputPath.equals("")) {

String realName = fileName.substring(fileName.lastIndexOf("/"),

fileName.lastIndexOf("."));

if (outputPath.charAt(outputPath.length()) == '/') {

swfFile = new File(outputPath + realName + ".swf");

} else {

swfFile = new File(outputPath + realName + ".swf");

}

}

}

}

调用转换类只需要传word、ptt、excel、pdf文件所在的路径参数就可以了。

展示在线预览的jsp代码如下:

StringswfFilePath=session.getAttribute("swfpath").toString();

System.out.println("展示路径"+swfFilePath);%>

html,body{height:100%;

}body{margin:0;padding:0;overflow:auto;

}#flashContent{display:none;

}

在线文档预览

varfp=newFlexPaperViewer('FlexPaperViewer','viewerPlaceHolder',{config:{SwfFile:escape(''),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:'en_US'}});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值