zoukankan      html  css  js  c++  java
  • java+OpenOffice 实现office转成pdf

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Scanner;
    
    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.StreamOpenOfficeDocumentConverter;
    
    public class Doc2PdfHtmlUtil {
        private static final String servicePath = "C:\Program Files (x86)\OpenOffice 4\";
    
        public static void main(String[] args) throws Exception {
    //        startOpenOfficeService();
            String file = "d:/33.pptx";
            InputStream inputStream = new FileInputStream(new File(file));
            file2pdf(inputStream, "d:/", "ppt");
    //        shutdownOpenOfficeService();
        }
    
        /**
         * 转换文件成pdf
         *
         * @param fromFileInputStream:
         * @throws IOException
         */
        public static String file2pdf(InputStream fromFileInputStream, String toFilePath, String type) throws Exception {
            String timesuffix = String.valueOf(System.currentTimeMillis());
            String docFileName = null;
            String htmFileName = null;
            if ("doc".equals(type)) {
                docFileName = timesuffix.concat(".doc");
                htmFileName = timesuffix.concat(".pdf");
            } else if ("docx".equals(type)) {
                docFileName = timesuffix.concat(".docx");
                htmFileName = timesuffix.concat(".pdf");
            } else if ("xls".equals(type)) {
                docFileName = timesuffix.concat(".xls");
                htmFileName = timesuffix.concat(".pdf");
            } else if ("xlsx".equals(type)) {
                docFileName = timesuffix.concat(".xlsx");
                htmFileName = timesuffix.concat(".pdf");
            } else if ("ppt".equals(type)) {
                docFileName = timesuffix.concat(".ppt");
                htmFileName = timesuffix.concat(".pdf");
            } else if ("pptx".equals(type)) {
                docFileName = timesuffix.concat(".pptx");
                htmFileName = timesuffix.concat(".pdf");
            } else if ("txt".equals(type)) {
                docFileName = timesuffix.concat(".txt");
                htmFileName = timesuffix.concat(".pdf");
            } else {
                return null;
            }
    
            File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
            File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
            if (htmlOutputFile.exists()) {
                htmlOutputFile.delete();
            }
    
            htmlOutputFile.createNewFile();
    
            docInputFile.createNewFile();
    
            /**
             * 由fromFileInputStream构建输入文件
             */
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            OutputStream os = new FileOutputStream(docInputFile);
            while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fromFileInputStream.close();
    
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            connection.connect();
            // convert
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(docInputFile, htmlOutputFile);
            connection.disconnect();
            // 转换完之后删除word文件
            docInputFile.delete();
            System.out.println("---------------执行完成---");
            return htmFileName;
        }
    
        /**
         * 文件转换成Html
         * 
         * @param fromFileInputStream
         * @param toFilePath
         * @param type
         * @return
         * @throws Exception
         */
        public static String file2Html(InputStream fromFileInputStream, String toFilePath, String type) throws Exception {
            String timesuffix = String.valueOf(System.currentTimeMillis());
            String docFileName = null;
            String htmFileName = null;
            if ("doc".equals(type)) {
                docFileName = timesuffix.concat(".doc");
                htmFileName = timesuffix.concat(".html");
            } else if ("docx".equals(type)) {
                docFileName = timesuffix.concat(".docx");
                htmFileName = timesuffix.concat(".html");
            } else if ("xls".equals(type)) {
                docFileName = timesuffix.concat(".xls");
                htmFileName = timesuffix.concat(".html");
            } else if ("xlsx".equals(type)) {
                docFileName = timesuffix.concat(".xlsx");
                htmFileName = timesuffix.concat(".html");
            } else if ("ppt".equals(type)) {
                docFileName = timesuffix.concat(".ppt");
                htmFileName = timesuffix.concat(".html");
            } else if ("pptx".equals(type)) {
                docFileName = timesuffix.concat(".pptx");
                htmFileName = timesuffix.concat(".html");
            } else if ("txt".equals(type)) {
                docFileName = timesuffix.concat(".txt");
                htmFileName = timesuffix.concat(".html");
            } else if ("pdf".equals(type)) {
                docFileName = timesuffix.concat(".pdf");
                htmFileName = timesuffix.concat(".html");
            } else {
                return null;
            }
            File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
            File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
            if (htmlOutputFile.exists()) {
                htmlOutputFile.delete();
            }
            htmlOutputFile.createNewFile();
            docInputFile.createNewFile();
            /**
             * 由fromFileInputStream构建输入文件
             */
            int bytesRead = 0;
            byte[] buffer = new byte[1024 * 8];
            OutputStream os = new FileOutputStream(docInputFile);
            while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fromFileInputStream.close();
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            connection.connect();
            // convert
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(docInputFile, htmlOutputFile);
            connection.disconnect();
            // 转换完之后删除word文件
            docInputFile.delete();
            return htmFileName;
        }
    
        public static void startOpenOfficeService() {
            String command = servicePath
                    + "program\soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard";
            try {
                Process pro = Runtime.getRuntime().exec(command);
            } catch (IOException e) {
                System.out.println("OpenOffice服务启动失败");
            }
        }
    
        /**
        *关闭代码
        **/
        public static void shutdownOpenOfficeService() {
        Scanner in = null;
           try {
               Process pro = Runtime.getRuntime().exec("tasklist");
               in = new Scanner(pro.getInputStream());
               while(in.hasNext()) {
                   String proString = in.nextLine();
                   if(proString.contains("soffice.exe")) {
                       String cmd = "taskkill /f /im soffice.exe";
                       pro = Runtime.getRuntime().exec(cmd);
                       System.out.println("soffice.exe关闭");
                   }
                   if(proString.contains("soffice.bin")) {
                       String cmd = "taskkill /f /im soffice.bin";
                       pro = Runtime.getRuntime().exec(cmd);
                       System.out.println("soffice.bin关闭");
                   }
               }
           } catch (IOException e) {
            e.printStackTrace();
           } finally {
               if(in != null) {
                   in.close();
               }
           }
        }
            
    }
  • 相关阅读:
    敏捷开发学习笔记-Agile development(AM)
    WindowsServer --------- 在服务器中安装sqlserver 数据库
    WIndowsServer ---------- 将本地文件映射到服务器
    windowsServer-------- 系统中调出文件扩展名
    SQLServer -------- 解决忘记sa 密码,创建一个新的
    SQLServer ---------- 附加数据库,以及解决附加时出现错误
    AJAX -------------- 如何使用ajax
    网络基础 ----------- osi 与 一些协议
    装系统---------- u盘 安装系统
    网络基础 ----------- 电脑设置为wifi站点
  • 原文地址:https://www.cnblogs.com/shihaiming/p/12992486.html
Copyright © 2011-2022 走看看