Java實現word文檔轉換爲pdf,jodconverter

首先下載openffice官網https://www.openoffice.org/ 版本:Apache_OpenOffice_4.1.5_Win_x86_install_zh-CN.exe

下載openOffice需要在C:\Program Files (x86)\OpenOffice 4\program
輸入命令soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

下載jodconverter的jar

https://pan.baidu.com/s/1ay3QVL3-uCdKk_vLlTHG7g

代碼直接用

package test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.regex.Pattern;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

public class OpenOffice {

    /**
     * office中各種格式
     */
    private static final String[] OFFICE_POSTFIXS = { "doc", "docx", "xls",
            "xlsx", "ppt", "pptx" };
    private ArrayList<String> Office_Formats = new ArrayList<String>();

    /**
     * pdf格式
     */
    private static final String PDF_POSTFIX= "pdf";
    
    /**
     * 根據操作系統的名稱,獲取OpenOffice.org 4的安裝目錄 如我的OpenOffice.org 4安裝在:C:/Program
     * Files/OpenOffice.org 4
     */
    
    public String getOfficeHome() {
        String osName = System.getProperty("os.name");
        if (Pattern.matches("Linux.*", osName)) {
            return "/opt/openoffice.org4";
        } else if (Pattern.matches("Windows.*", osName)) {
            return "C:\\Program Files (x86)\\OpenOffice 4";
        }
        return null;
    }
    /**
     * 轉換文件
     * @param inputFilePath
     * @param outputFilePath
     * @param converter
     */
    public void converterFile(String inputFilePath, String outputFilePath,
            OfficeDocumentConverter converter) {
        File inputFile=new File(inputFilePath);
        File outputFile = new File(outputFilePath);
        // 假如目標路徑不存在,則新建該路徑
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        converter.convert(inputFile, outputFile);
        System.out.println("文件:" + inputFilePath + "\n轉換爲\n目標文件:" + outputFile
                + "\n成功!");
    }

    /**
     * 使Office2003-2007全部格式的文檔(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 轉化爲pdf文件
     * 
     * @param inputFilePath
     *            源文件路徑,如:"e:/test.docx"
     * @param outputFilePath
     *            如果指定則按照指定方法,如果未指定(null)則按照源文件路徑自動生成目標文件路徑,如:"e:/test_docx.pdf"
     * @return
     */
    public boolean openOffice2Pdf(String inputFilePath, String outputFilePath) {
        boolean flag = false;
        /*
         * 連接OpenOffice.org 並且啓動OpenOffice.org
         */
        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
        // 獲取OpenOffice.org 4的安裝目錄
        String officeHome = getOfficeHome();
        config.setOfficeHome(officeHome);
        // 啓動OpenOffice的服務
        OfficeManager officeManager = config.buildOfficeManager();
        officeManager.start();
        // 連接OpenOffice
        OfficeDocumentConverter converter = new OfficeDocumentConverter(
                officeManager);
        long begin_time = new Date().getTime();
        File inputFile=new File(inputFilePath);
        Collections.addAll(Office_Formats, OFFICE_POSTFIXS);
        if ((null != inputFilePath) && (inputFile.exists())) {
            // 判斷目標文件路徑是否爲空
            if (Office_Formats.contains(getPostfix(inputFilePath))) {
                if (null == outputFilePath) {
                    // 轉換後的文件路徑
                    String outputFilePath_new = generateDefaultOutputFilePath(inputFilePath);
                    converterFile(inputFilePath, outputFilePath_new, converter);
                    flag = true;

                } else {
                    converterFile(inputFilePath, outputFilePath, converter);
                    flag = true;
                }
            }

        } else {
            System.out.println("找不到資源");
        }
        long end_time = new Date().getTime();
        System.out.println("文件轉換耗時:[" + (end_time - begin_time) + "]ms");
        officeManager.stop();
        return flag;
    }

    /**
     * 如果未設置輸出文件路徑則按照源文件路徑和文件名生成輸出文件地址。例,輸入爲 D:/fee.xlsx 則輸出爲D:/fee_xlsx.pdf
     */
    public String generateDefaultOutputFilePath(String inputFilePath) {
        String outputFilePath = inputFilePath.replaceAll("."
                + getPostfix(inputFilePath), "_" + getPostfix(inputFilePath)
                + ".pdf");
        return outputFilePath;
    }

    /**
     * 獲取inputFilePath的後綴名,如:"e:/test.pptx"的後綴名爲:"pptx"
     */
    public String getPostfix(String inputFilePath) {
        String[] p = inputFilePath.split("\\.");
        if (p.length > 0) {// 判斷文件有無擴展名
            // 比較文件擴展名
            return p[p.length - 1];
        } else {
            return null;
        }
    }
    
    public static void main(String[] args) {

    	OpenOffice office2pdf = new OpenOffice();
        office2pdf.openOffice2Pdf("D:/可實現功能.docx","D:/可實現功能_" + new Date().getTime() + "."+ PDF_POSTFIX);
        office2pdf.openOffice2Pdf("D:/新建123工作表.xls",null);
    }


}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章