package com.qxueyou.scc.base.util;
|
|
import java.io.File;
|
|
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;
|
|
/**
|
* 文件转换为pdf的工具类
|
*
|
* @author lyl
|
*
|
*/
|
public class docConverterUtil {
|
@SuppressWarnings("unused")
|
private String fileString; // (待转换文件的根路径)
|
@SuppressWarnings("unused")
|
private String outputPath = ""; // pdf输出路径 ,如果不设置就输出在默认的位置
|
@SuppressWarnings("unused")
|
private String fileName;
|
private File pdfFile;
|
private File docFile;
|
|
public docConverterUtil(String fileString) {
|
ini(fileString);
|
}
|
|
/**
|
* 可自定义设置转换文件全路径名
|
*
|
* @param fileString
|
*/
|
public void setFile(String fileString) {
|
ini(fileString);
|
}
|
|
/**
|
* 初始化
|
*
|
* @param fileString
|
*/
|
private void ini(String fileString) {
|
this.fileString = fileString;
|
docFile = new File(fileString);
|
}
|
|
/**
|
* 设置输出路径
|
*/
|
public void setOutputPath(String outputPath) {
|
this.outputPath = outputPath;
|
if (!outputPath.equals("")) {
|
pdfFile = new File(outputPath + ".pdf");
|
/*
|
* System.out.println(fileName); String realName =
|
* fileString.substring(fileName.lastIndexOf("/")+1);
|
* System.out.println(realName); if (outputPath.charAt(outputPath.length()-1) ==
|
* '/') { pdfFile = new File(outputPath + realName + ".pdf"); } else { pdfFile =
|
* new File(outputPath+"/" + realName + ".pdf"); }
|
*/
|
}
|
}
|
|
/**
|
* 转为PDF
|
*
|
* @param file
|
*/
|
@SuppressWarnings("unused")
|
public void conver() throws Exception {
|
if (docFile.exists()) {
|
if (!pdfFile.exists()) {
|
String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4";
|
// 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
|
if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
|
OpenOffice_HOME += "\\";
|
}
|
// 启动OpenOffice的服务
|
String command = OpenOffice_HOME
|
+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
|
Process pro = Runtime.getRuntime().exec(command);
|
|
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
|
try {
|
connection.connect();
|
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
|
converter.convert(docFile, pdfFile);
|
connection.disconnect();
|
System.out.println("df转换成功,保存路径:" + pdfFile.getPath());
|
} catch (java.net.ConnectException e) {
|
e.printStackTrace();
|
System.out.println("转换失败,openoffice服务未启动!");
|
throw e;
|
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
|
e.printStackTrace();
|
System.out.println("读取转换文件失败");
|
throw e;
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw e;
|
}
|
} else {
|
System.out.println("文件已存在,不需要转换");
|
}
|
} else {
|
System.out.println("需要转换的文档不存在,无法转换");
|
}
|
}
|
|
/**
|
* 返回pdf文件路径
|
*
|
* @return
|
*/
|
public String getpdfPath() {
|
if (pdfFile.exists()) {
|
String tempString = pdfFile.getPath();
|
tempString = tempString.replaceAll("\\\\", "/");
|
return tempString;
|
} else {
|
return "";
|
}
|
}
|
|
public static void main(String[] args) throws Exception {
|
String fileString = "D:/upload/ppt4.ppt";
|
|
docConverterUtil dcu = new docConverterUtil(fileString);
|
dcu.setOutputPath("D:/upload/a");
|
dcu.conver();
|
|
System.out.println(dcu.getpdfPath());
|
|
}
|
}
|