使用OpenOffice實現文檔預覽


概述

使用OpenOffice將 office文檔轉為pdf,然后再將pdf轉為圖片,實現文檔預覽的功能。

依賴組件

OpenOffice.org或者LibreOffice

JODConverter

需要注意的問題

Linux平台下程序的兼容性

防止轉換出來的文檔亂碼

防止文檔轉換不全,一些特殊格式的處理

OpenOffice

1.啟動OpenOffice的服務

cd C:\Program Files (x86)\OpenOffice 4\program

執行命令

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

進程名稱是soffice.exe

 

查看是否安裝成功,查看端口對應的pid

netstat -ano|findstr "8100"

 

Pdf 文檔轉圖片

pdf轉高清圖片需要的jar:http://download.csdn.net/detail/emoven/9666543

代碼

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;

public class Main {

    public static void main(String[] args) {
        int result = office2PDF("D:\\RabbitMQ的實戰應用.pptx","D:\\RabbitMQ的實戰應用.pdf");
        System.out.println(result);

        pdf2Pic("D:\\RabbitMQ的實戰應用.pdf", "D:\\22\\");
    }

    public static void pdf2Pic(String pdfPath, String path){
        Document document = new Document();
        document.setFile(pdfPath);
        float scale = 2.5f;//縮放比例
        float rotation = 0f;//旋轉角度

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage)
                    document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                String imgName = i + ".png";
                System.out.println(imgName);
                File file = new File(path + imgName);
                ImageIO.write(rendImage, "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
        }
        document.dispose();
    }

    /**
     * 將Office文檔轉換為PDF. 運行該函數需要用到OpenOffice, OpenOffice下載地址為
     * http://www.openoffice.org/
     *
     * <pre>
     * 方法示例:
     * String sourcePath = "F:\\office\\source.doc";
     * String destFile = "F:\\pdf\\dest.pdf";
     * Converter.office2PDF(sourcePath, destFile);
     * </pre>
     *
     * @param sourceFile
     *            源文件, 絕對路徑. 可以是Office2003-2007全部格式的文檔, Office2010的沒測試. 包括.doc,
     *            .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
     * @param destFile
     *            目標文件. 絕對路徑. 示例: F:\\pdf\\dest.pdf
     * @return 操作成功與否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置錯誤; 如果返回 0,
     *         則表示操作成功; 返回1, 則表示轉換失敗
     */
    public static int office2PDF(String sourceFile, String destFile) {
        try {
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                return -1;// 找不到源文件, 則返回-1
            }

            // 如果目標路徑不存在, 則新建該路徑
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }

            // connect to an OpenOffice.org instance running on port 8100
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                    "127.0.0.1", 8100);
            connection.connect();

            // convert
            DocumentConverter converter = new OpenOfficeDocumentConverter(
                    connection);
            converter.convert(inputFile, outputFile);

            // close the connection
            connection.disconnect();

            return 0;
        } catch (ConnectException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 1;
    }
}

 

遇到的問題

1. Exception in thread "main" java.lang.IllegalArgumentException: unknown document format for file: F:\RESTful接口設計規范.docx

不能識別docx, xlsx等文件

 java使用openoffice將office系列文檔轉換為PDF

通過改JODConverter的源碼使得其支持docx等類型的文件。

或者

將Office(如:Word、Excel、PPT 等)文件轉html(通過OpenOffice實現)

使用JODConverter3.0版本,與2.2.2版本區別較大

 

總結

將文檔轉為html的格式沒有轉為pdf的格式好

參考

工具網站


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM