天天看點

Java使用Openoffice将word、ppt轉換為PDF

最近項目中要實作WORD的檔案預覽功能,我們可以通過将WORD轉換成PDF或者HTML,然後通過浏覽器預覽。

OpenOffice

OpenOffice.org 是一套跨平台的辦公室軟體套件,能在 Windows、Linux、MacOS X (X11)、和 Solaris 等作業系統上執行。它與各個主要的辦公室軟體套件相容。OpenOffice.org 是自由軟體,任何人都可以免費下載下傳、使用、及推廣它。

下載下傳位址

http://www.openoffice.org/

JodConverter

jodconverter-2.2.2.zip 下載下傳位址:

http://sourceforge.net/projects/jodconverter/files/JODConverter/

Word轉換

啟動OpenOffice的服務

進入openoffice安裝目錄,通過cmd啟動一個soffice服務,啟動的指令是

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"

如果覺得背景運作OpenOffice服務比較麻煩,可以通過

運作代碼

public class PDFDemo {

    public static boolean officeToPDF(String sourceFile, String destFile) {
        try {

            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                // 找不到源檔案, 則傳回false
                return false;
            }
            // 如果目标路徑不存在, 則建立該路徑
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            //如果目标檔案存在,則删除
            if (outputFile.exists()) {
                outputFile.delete();
            }
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            connection.connect();
            //用于測試openOffice連接配接時間
            System.out.println("連接配接時間:" + df.format(new Date()));
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
                    connection);
            converter.convert(inputFile, outputFile);
            //測試word轉PDF的轉換時間
            System.out.println("轉換時間:" + df.format(new Date()));
            connection.disconnect();
            return true;
        } catch (ConnectException e) {
            e.printStackTrace();
            System.err.println("openOffice連接配接失敗!請檢查IP,端口");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        officeToPDF("E:\\test.docx", "E:\\test.pdf");
    }
}
           

Word、ppt轉Html

隻需要将字尾名從

.pdf

改為

.html

即可。

public static void main(String[] args) {
    officeToPDF("E:\\test.docx", "E:\\test.html");
}
           

Maven配置

Maven依賴

<dependency>
	<groupId>com.artofsolving</groupId>
	<artifactId>jodconverter</artifactId>
	<version>2.2.1</version>
</dependency>
<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>jurt</artifactId>
	<version>3.0.1</version>
</dependency>
<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>ridl</artifactId>
	<version>3.0.1</version>
</dependency>
<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>juh</artifactId>
	<version>3.0.1</version>
</dependency>
<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>unoil</artifactId>
	<version>3.0.1</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-jdk14</artifactId>
	<version>1.4.3</version>
</dependency>
           

Maven隻有 2.2.1版本,2.2.1版本有一個問題,那就是不相容docx和pptx,如果你們不使用jodconverter-2.2.2 中lib,而想要使用2.2.1版本,需要修改一下

BasicDocumentFormatRegistry

類中的

getFormatByFileExtension

方法:

  1. 建立包

    com.artofsolving.jodconverter

  2. 建立類

    BasicDocumentFormatRegistry

    ,複制下面代碼
package com.artofsolving.jodconverter;

/**
 * @author 李文浩
 * @date 2017/12/25
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
    private List documentFormats = new ArrayList();

    public BasicDocumentFormatRegistry() {
    }

    public void addDocumentFormat(DocumentFormat documentFormat) {
        this.documentFormats.add(documentFormat);
    }

    protected List getDocumentFormats() {
        return this.documentFormats;
    }

    public DocumentFormat getFormatByFileExtension(String extension) {
        if (extension == null) {
            return null;
        } else {
            if (extension.indexOf("doc") >= 0) {
                extension = "doc";
            }
            if (extension.indexOf("ppt") >= 0) {
                extension = "ppt";
            }
            if (extension.indexOf("xls") >= 0) {
                extension = "xls";
            }
            String lowerExtension = extension.toLowerCase();
            Iterator it = this.documentFormats.iterator();

            DocumentFormat format;
            do {
                if (!it.hasNext()) {
                    return null;
                }

                format = (DocumentFormat)it.next();
            } while(!format.getFileExtension().equals(lowerExtension));

            return format;
        }
    }

    public DocumentFormat getFormatByMimeType(String mimeType) {
        Iterator it = this.documentFormats.iterator();

        DocumentFormat format;
        do {
            if (!it.hasNext()) {
                return null;
            }

            format = (DocumentFormat)it.next();
        } while(!format.getMimeType().equals(mimeType));

        return format;
    }
}
           

下面是增加的部分,僅僅增加了将docx按照doc的處理方式處理。而2.2.2版本已經預設增加了。

if (extension.indexOf("doc") >= 0) {
    extension = "doc";
}
if (extension.indexOf("ppt") >= 0) {
    extension = "ppt";
}
if (extension.indexOf("xls") >= 0) {
    extension = "xls";
}
           

參考文檔:

  1. Java實作線上預覽–openOffice實作
  2. Java項目中使用OpenOffice轉PDF
  3. java使用openoffice将office系列文檔轉換為PDF
  4. java 如何将 word,excel,ppt如何轉pdf--jacob
  5. java 如何将 word,excel,ppt如何轉pdf --openoffice (1)