zoukankan      html  css  js  c++  java
  • 使用OpenOffice实现文档预览

    概述

    使用OpenOffice将 office文档转为pdf,然后再将pdf转为图片,实现文档预览的功能。

    依赖组件

    OpenOffice.org或者LibreOffice

    JODConverter

    需要注意的问题

    Linux平台下程序的兼容性

    防止转换出来的文档乱码

    防止文档转换不全,一些特殊格式的处理

    OpenOffice

    1.启动OpenOffice的服务

    cd C:Program Files (x86)OpenOffice 4program

    执行命令

    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的格式好

    参考

    工具网站

  • 相关阅读:
    面向接口程序设计思想实践
    Block Chain Learning Notes
    ECMAScript 6.0
    Etcd Learning Notes
    Travis CI Build Continuous Integration
    Markdown Learning Notes
    SPRING MICROSERVICES IN ACTION
    Java Interview Questions Summary
    Node.js Learning Notes
    Apache Thrift Learning Notes
  • 原文地址:https://www.cnblogs.com/tonyq/p/7586122.html
Copyright © 2011-2022 走看看