zoukankan      html  css  js  c++  java
  • OpenOffice--Office转PDF

    import java.io.File;
    
    import org.artofsolving.jodconverter.OfficeDocumentConverter;
    import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
    import org.artofsolving.jodconverter.office.OfficeManager;
    
    public class OpenOfficePDFConverter {
        private static OfficeManager officeManager;
    //    private static String OFFICE_HOME = "C:\Program Files (x86)\OpenOffice 4";
        private static String OFFICE_HOME = "C:\Program Files\LibreOffice";
        private static int port[] = { 9989 };
        
        public static void main(String[] args) {
            try{
                startService();
                convert2PDF("C:\Users\12614\Desktop\test1.docx");
            }catch (Exception e) {
                e.printStackTrace();
            } finally {
                stopService();
            }
        }
    
        public static void convert2PDF(String inputFile, String pdfFile) {
            OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
            converter.convert(new File(inputFile), new File(pdfFile));
        }
    
        public static void convert2PDF(String inputFile) {
            convert2PDF(inputFile, inputFile.substring(0, inputFile.lastIndexOf(".")) + ".pdf");
        }
    
        public static void startService() {
            DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
            try {
                System.out.println("准备启动服务....");
                configuration.setOfficeHome(OFFICE_HOME);
                // 设置OpenOffice.org安装目录
                configuration.setPortNumbers(port);
                // 设置转换端口,默认为8100
                configuration.setTaskExecutionTimeout(1000 * 60 * 5L);
                // 设置任务执行超时为5分钟
                configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
                // 设置任务队列超时为24小时
    
                officeManager = configuration.buildOfficeManager();
                officeManager.start();
                // 启动服务
                System.out.println("office转换服务启动成功!");
            } catch (Exception ce) {
                ce.printStackTrace();
            }
        }
        
        public static void stopService() {
            if (officeManager != null) {
                officeManager.stop();
            }
            System.out.println("关闭office转换成功!");
        }
    }
  • 相关阅读:
    HDU 1104 Remainder (POJ 2426 BFS+数论)
    POJ 1398 Complete the sequence! ★ (差分)
    POJ 2917 Diophantus of Alexandria ★(数论)
    POJ 1948 Triangular Pastures (二维01背包)
    POJ 1948 Triangular Pastures (二维01背包)
    starter kits 介绍
    starter kits 介绍
    信息分析与预测复习题(绪论)
    信息分析与预测复习题(绪论)
    starter kits 介绍
  • 原文地址:https://www.cnblogs.com/chenss15060100790/p/10026845.html
Copyright © 2011-2022 走看看