| | |
| | | import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; |
| | | |
| | | /** |
| | | * 文件转换为pdf的工具类 |
| | | * 文件转换为pdf的工具类 |
| | | * |
| | | * @author lyl |
| | | * |
| | | */ |
| | | public class docConverterUtil { |
| | | @SuppressWarnings("unused") |
| | | private String fileString; // (待转换文件的根路径) |
| | | private String fileString; // (待转换文件的根路径) |
| | | @SuppressWarnings("unused") |
| | | private String outputPath = ""; // pdf输出路径 ,如果不设置就输出在默认的位置 |
| | | private String outputPath = ""; // pdf输出路径 ,如果不设置就输出在默认的位置 |
| | | @SuppressWarnings("unused") |
| | | private String fileName; |
| | | private File pdfFile; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 可自定义设置转换文件全路径名 |
| | | * 可自定义设置转换文件全路径名 |
| | | * |
| | | * @param fileString |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 初始化 |
| | | * 初始化 |
| | | * |
| | | * @param fileString |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 设置输出路径 |
| | | * 设置输出路径 |
| | | */ |
| | | public void setOutputPath(String outputPath) { |
| | | this.outputPath = outputPath; |
| | |
| | | } |
| | | |
| | | /** |
| | | * תΪPDF |
| | | * 转为PDF |
| | | * |
| | | * @param file |
| | | */ |
| | |
| | | if (docFile.exists()) { |
| | | if (!pdfFile.exists()) { |
| | | String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4"; |
| | | // 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\' |
| | | // 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\' |
| | | if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') { |
| | | OpenOffice_HOME += "\\"; |
| | | } |
| | | // 启动OpenOffice的服务 |
| | | // 启动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); |
| | |
| | | DocumentConverter converter = new OpenOfficeDocumentConverter(connection); |
| | | converter.convert(docFile, pdfFile); |
| | | connection.disconnect(); |
| | | System.out.println("df转换成功,保存路径:" + pdfFile.getPath()); |
| | | System.out.println("df转换成功,保存路径:" + pdfFile.getPath()); |
| | | } catch (java.net.ConnectException e) { |
| | | e.printStackTrace(); |
| | | System.out.println("转换失败,openoffice服务未启动!"); |
| | | System.out.println("转换失败,openoffice服务未启动!"); |
| | | throw e; |
| | | } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { |
| | | e.printStackTrace(); |
| | | System.out.println("读取转换文件失败"); |
| | | System.out.println("读取转换文件失败"); |
| | | throw e; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } |
| | | } else { |
| | | System.out.println("文件已存在,不需要转换"); |
| | | System.out.println("文件已存在,不需要转换"); |
| | | } |
| | | } else { |
| | | System.out.println("需要转换的文档不存在,无法转换"); |
| | | System.out.println("需要转换的文档不存在,无法转换"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 返回pdf文件路径 |
| | | * 返回pdf文件路径 |
| | | * |
| | | * @return |
| | | */ |