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转换成功!");
    }
}

 

posted on 2018-11-27 15:54  疯狂的妞妞  阅读(339)  评论(0编辑  收藏  举报

导航