openOffice插件将office文件转成PDF文件

利用openOffice插件将office文件转成PDF文件实现浏览在线预览效果

    public static boolean office2PDF(String sourceFile, String destFile) {
        try {
            // 1 找不到源文件, 则返回false
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                return false;
            }
            File outputFile = new File(destFile);
            // 2 已经存在pdf文件,返回成功
            if (outputFile.exists())
                return true;

            // 3 如果目标路径不存在, 则新建该路径
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }

            // 4 从url.properties文件中读取OpenOffice的安装根目录, OpenOffice_HOME对应的键值.
            Properties prop = new Properties();
            FileInputStream fis = null;
            fis = new FileInputStream(RUL_PATH); // 属性文件输入流
            prop.load(fis); // 将属性文件流装载到Properties对象中
            fis.close(); // 关闭流

            Process pro=null;
            OpenOfficeConnection connection=null;
            String System = prop.getProperty("System");
            
            if(System.equals("WINDOWS")){
                String OpenOffice_WINDOWS_HOME = prop.getProperty("OpenOffice_WINDOWS_HOME");
                String OpenOffice_WINDOWS_PORT = prop.getProperty("OpenOffice_WINDOWS_PORT");
                // 5 如果没有配置openoffice Home  ,返回错误
                if (OpenOffice_WINDOWS_HOME == null||OpenOffice_WINDOWS_PORT==null)
                    return false;
                int port=0;
                try {
                    port = Integer.parseInt(OpenOffice_WINDOWS_PORT);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                String command=linOpenOffice(OpenOffice_WINDOWS_HOME);
                pro = Runtime.getRuntime().exec(command);    
                connection = new SocketOpenOfficeConnection("127.0.0.1", port);                
            }else {
                String OpenOffice_LINUX_HOME = prop.getProperty("OpenOffice_LINUX_HOME");
                String OpenOffice_LINUX_PORT = prop.getProperty("OpenOffice_LINUX_PORT");
                // 5 如果没有配置openoffice Home  ,返回错误
                if (OpenOffice_LINUX_PORT == null||OpenOffice_LINUX_PORT==null)
                    return false;
                int port=0;
                try {
                    port = Integer.parseInt(OpenOffice_LINUX_PORT);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                String command=linOpenOffice(OpenOffice_LINUX_HOME);
                pro = Runtime.getRuntime().exec(command);    
                connection = new SocketOpenOfficeConnection("127.0.0.1", port);            
            }
            connection.connect();
            // 8 转换pdf
            DocumentConverter converter = new OpenOfficeDocumentConverter(
                    connection);
            converter.convert(inputFile, outputFile);

            // 9 关闭连接
            connection.disconnect();
            // 10 关闭OpenOffice服务的进程 , 避免占用CPU
            pro.destroy();

            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ConnectException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
      /**
     * linux启动openOffice
     * @param OpenOffice_HOME
     * @return
     * @throws IOException
     */
    private static String linOpenOffice(String OpenOffice_LINUX_HOME) throws IOException {
        // 6 如果从文件中读取的URL地址最后一个字符不是 '/',则添加'/'
        if (OpenOffice_LINUX_HOME.charAt(OpenOffice_LINUX_HOME.length() - 1) != '/') {
            OpenOffice_LINUX_HOME += "/";
        }
        //  启动OpenOffice的服务 , 注意端口不要冲突
        String command = OpenOffice_LINUX_HOME                    
                            +"program/soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
        
        return command;
    }
    /**
     * windows启动openOffice
     * @param OpenOffice_HOME
     * @return
     * @throws IOException
     */
    private static     String  winOpenOffice(String OpenOffice_WINDOWS_HOME) throws IOException {
        // 6 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
        if (OpenOffice_WINDOWS_HOME.charAt(OpenOffice_WINDOWS_HOME.length() - 1) != '\\') {
            OpenOffice_WINDOWS_HOME += "\\";
        }
        
        //7 启动OpenOffice的服务 , 注意端口不要冲突
        String command = OpenOffice_WINDOWS_HOME
                            + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8300;urp;\" -nofirststartwizard";
        
        return command;
    }

插件路径配置文件

读取配置文件
        /**
     * 环境变量下面的openoffice_url.properties的绝对路径
     */
    private static final String RUL_PATH = Thread.currentThread()
            .getContextClassLoader().getResource("").getPath()
            .replace("%20", " ")
            + "openoffice_url.properties";

配置文件内容:

OpenOffice_WINDOWS_HOME=C:\\Program Files (x86)\\OpenOffice 4
OpenOffice_WINDOWS_PORT=8300
System=WINDOWS

#OpenOffice_LINUX_HOME=/opt/openoffice4
#OpenOffice_LINUX_PORT=8100
#System=LINUX

#office转PDF路径

 

posted @ 2019-07-26 15:04  海绵-宝宝  阅读(713)  评论(0编辑  收藏  举报