您的位置:首页 > 运维架构

Openoffice+swfTools+flexPaper实现在线预览

2014-11-05 17:36 369 查看
一   原理:

1.通过第三方工具OpenOffice将Word、Excel、ppt、txt等文件转换为pdf文件

2.swftools讲pdf文件转换为swf格式的文件

3.通过flexpaper文档组件在页面上进行展示

二 .我安装的版本:

Openoffice4.1+swfTools0.92+FlexPaper1.5.1

三 注意:

Oppenoffice安装完成后,需要将它的服务打开:打开DOS窗口,进入OpenOffice安装盘,输入以下代码启动:Openoffice\program\soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

四 核心代码:

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

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;

import com.eda.test.common.ConStant;

import com.eda.test.util.ImageUtil;

public class OpenOfficePDFConverter {

/**
* 涉及pdf2swf路径问题
*/
private final String CONVERTFILETYPE = "jpg,jpeg,font,gif,png,wav";
//private String swftoolsPath="D:/SWFTools/"; // swftools工具的安装路径
private static String PDF2SWF_PATH = ConStant.SWFTOOLS_PDF2SWF_PATH;
private String fileName;
private String fileType;
private File pdfFile;
private File swfFile;
private File srcFile;
private static final int BUFFER_SIZE = 16 * 1024;

/*
* 转换为pdf文件
*/
public boolean convert(String fileString, String fileTomcat){
fileString=fileTomcat+fileString;
String newfile = "";
fileString = fileString.replace("/", "\\").replace("\\", "\\\\");
fileName = fileString.substring(0,fileString.lastIndexOf("."));
if(swfFile.exists()){
return true;
}
//this.swftoolsPath = fileTomcat + "\\flexpaper\\SWFTools\\";

fileType = fileString.substring(fileString.lastIndexOf(".") + 1).toLowerCase();
// 设置txt格式的转换命令
if (fileType.equals("txt")) {
newfile = fileString.substring(0, fileString.lastIndexOf("."))
+ ".odt";
}
// 设置wps格式的转换命令
if (fileType.equals("wps")) {
newfile = fileString.substring(0, fileString.lastIndexOf("."))
+ ".doc";
}
// 设置et格式的转换命令
if (fileType.equals("et")) {
newfile = fileString.substring(0, fileString.lastIndexOf("."))
+ ".xls";
}
// 设置et格式的转换命令
if (fileType.equals("dps")) {
newfile = fileString.substring(0, fileString.lastIndexOf("."))
+ ".ppt";
}

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

    InputStream in = null;

    OutputStream out = null;

    try {

    in = new BufferedInputStream(new FileInputStream(new File(fileString)), BUFFER_SIZE);

    out = new BufferedOutputStream(new FileOutputStream(new File(newfile)),

    BUFFER_SIZE);

    byte[] buffer = new byte[BUFFER_SIZE];

    int len = 0;

    while ((len = in.read(buffer)) > 0) {

    out.write(buffer, 0, len);

    }

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    if (null != in) {

    try {

    in.close();

    out.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

        }else{

        newfile=fileString;

        }

     // 设置png格式的转换命令
if (fileType.equals("png")||fileType.equals("gif")||fileType.equals("jpeg")||fileType.equals("jpg")) {
newfile = fileString.substring(0, fileString.lastIndexOf("."))
+ ".jpg";
fileType="jpeg";
System.out.println(fileString+"--------------");

// System.out.println(newfile);
try {
ImageUtil.CopyImageFromURL(fileString, newfile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// System.out.println(fileType);
}
srcFile = new File(newfile);
System.out.println(srcFile+"*********************");
pdfFile = new File(fileName + ".pdf");
System.out.println(pdfFile+".....................");
// 设置jpg图片格式的转换命令
if (fileType.equals("jpg")) {
fileType = "jpeg";
}

// System.out.println("-----");
boolean flag=false;
try {
System.out.println("3333");
System.out.println(CONVERTFILETYPE.indexOf(fileType)+":///////////////");
if(CONVERTFILETYPE.indexOf(fileType) == -1) {
System.out.println("444");
if (srcFile.exists()) {
if (!pdfFile.exists()) {
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
"127.0.0.1", 8100);
System.out.println("5555");
try {
connection.connect();
// 获取转换器
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
// 开始转换
converter.convert(srcFile, pdfFile);
System.out.println("成功 了,吗???");
// 转换成功,关闭连接
connection.disconnect();
flag=pdf2swf();
} catch (java.net.ConnectException e) {
System.out.println("****openoffice服务未启动!****");
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {

// e.printStackTrace();
System.out.println("****swf转换器异常,读取转换文件失败****"+e.getMessage());
<
b0c3
span style="white-space:pre;">}
} else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
flag=pdf2swf();
}
} else {
System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
}
}else{
System.out.println("66666666666");
flag=pdf2swf();
}
} catch (Exception e) {
e.printStackTrace();
flag=false;
}
return flag;
}

/*
* 转换成swf
*/
private boolean pdf2swf(){
boolean flag=false;

// System.out.println(swftoolsPath + fileType + "2swf.exe "

// + srcFile + " -o " + swfFile

// + " ");
try {
Runtime runtime = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
Process pro=runtime.exec(PDF2SWF_PATH + " -qG -s disablelinks -s languagedir=\""+PDF2SWF_PATH+"xpdf-chinese-simplified\" "
+ pdfFile.getPath() + " -o  "+swfFile.getPath()
+ "  -f -T 9 -t -s storeallcharacters -s poly2bitmap ");

// System.out.println(swftoolsPath + "pdf2swf.exe -qG -s disablelinks -s languagedir=\""+swftoolsPath+"xpdf-chinese-simplified\" "

// + pdfFile.getPath() + " -o  "+swfFile.getPath()

// + "  -f -T 9 -t -s storeallcharacters -s poly2bitmap ");

// BufferedReader bufferedReader = new BufferedReader(

// new InputStreamReader(pro.getInputStream()));

// while (bufferedReader.readLine() != null) {

//

// }
System.out.println("-------开始转换pdf转swf");
pro.waitFor();
if(!fileType.equals("pdf")&&pdfFile.exists()) {
pdfFile.delete();
}
System.out.println("-------2转换成功swf");
if(!swfFile.exists()){//没转换成功
flag=false;
}else{
flag=true;
}

// System.out.println("========");
} else if (CONVERTFILETYPE.indexOf(fileType) != -1) {
if("gif".equals(fileType)||"png".equals(fileType)){
Process pro=runtime.exec(PDF2SWF_PATH + fileType
+ srcFile + " -o " + swfFile
+ " ");
pro.waitFor();
}else{
Process pro=runtime.exec(PDF2SWF_PATH + fileType
+ srcFile + " -o " + swfFile
+ " -f -T 9 ");
System.out.println(PDF2SWF_PATH + fileType
+ srcFile + " -o " + swfFile
+ " -f -T 9 ");
pro.waitFor();
}

// System.out.println(swfFile.exists());
if(!swfFile.exists()){//没转换成功
flag=false;
}else{
flag=true;
}

// System.out.println(fileType+"===============");
}
} else {
flag=true;
System.out.println("****swf已存在不需要转换****");
}

} catch (Exception e) {
System.out.println("****pdf转换swf错误****");
}

        return flag;
}

}

/*
 * 测试
 */

public static void main(String[] args){
String fileString="upload/222.txt";
String fileTomcat="F:/文件预览/";
OpenOfficePDFConverter file=new OpenOfficePDFConverter();
System.out.println(file.convert(fileString,fileTomcat));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: