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();
               }
           }
        }
            
    }
  • 相关阅读:
    HDU 1003——Max Sum(动态规划)
    HDU 2602 ——背包问题
    HDU 1850——Being a good boy
    HDU——2588 数论应用
    HDU1222——数论
    HDU1465——不容易系列之一(错排)
    URAL 2038 Minimum Vertex Cover
    772002画马尾
    CodeForces 19D Points
    The 2015 China Collegiate Programming Contest Game Rooms
  • 原文地址:https://www.cnblogs.com/shihaiming/p/12992486.html
Copyright © 2011-2022 走看看