• 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();
               }
           }
        }
            
    }
  • 相关阅读:
    洛谷 P2048 [NOI2010]超级钢琴(优先队列,RMQ)
    洛谷P1074 靶形数独(跳舞链)
    洛谷P1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)
    洛谷P4003 无限之环(费用流)
    洛谷P3264 [JLOI2015]管道连接(斯坦纳树)
    洛谷P3190 [HNOI2007]神奇游乐园(插头dp)
    洛谷P3272 [SCOI2011]地板(插头dp)
    常用的端口配置
    Win7+Eclipse+Hadoop2.6.4开发环境搭建
    Windows10+eclipse+hadoop2.7.1环境配置+wordcount-折腾笔记
  • 原文地址:https://www.cnblogs.com/shihaiming/p/12992486.html
Copyright © 2020-2023  润新知