派生自 projectDept/qhighschool

Administrator
2022-11-21 8d0e57a64fe6c31559ffcf38859fb1f5084e1e23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.qxueyou.scc.base.util;
 
import java.io.File;
 
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;
 
/**
 * Îļþת»»ÎªpdfµÄ¹¤¾ßÀà
 * 
 * @author lyl
 *
 */
public class docConverterUtil {
    @SuppressWarnings("unused")
    private String fileString; // (´ýת»»ÎļþµÄ¸ù·¾¶)
    @SuppressWarnings("unused")
    private String outputPath = ""; // pdfÊä³ö·¾¶ £¬Èç¹û²»ÉèÖþÍÊä³öÔÚĬÈϵÄλÖÃ
    @SuppressWarnings("unused")
    private String fileName;
    private File pdfFile;
    private File docFile;
 
    public docConverterUtil(String fileString) {
        ini(fileString);
    }
 
    /**
     * ¿É×Ô¶¨ÒåÉèÖÃת»»Îļþȫ·¾¶Ãû
     * 
     * @param fileString
     */
    public void setFile(String fileString) {
        ini(fileString);
    }
 
    /**
     * ³õʼ»¯
     * 
     * @param fileString
     */
    private void ini(String fileString) {
        this.fileString = fileString;
        docFile = new File(fileString);
    }
 
    /**
     * ÉèÖÃÊä³ö·¾¶
     */
    public void setOutputPath(String outputPath) {
        this.outputPath = outputPath;
        if (!outputPath.equals("")) {
            pdfFile = new File(outputPath + ".pdf");
            /*
             * System.out.println(fileName); String realName =
             * fileString.substring(fileName.lastIndexOf("/")+1);
             * System.out.println(realName); if (outputPath.charAt(outputPath.length()-1) ==
             * '/') { pdfFile = new File(outputPath + realName + ".pdf"); } else { pdfFile =
             * new File(outputPath+"/" + realName + ".pdf"); }
             */
        }
    }
 
    /**
     * ×ªÎªPDF
     * 
     * @param file
     */
    @SuppressWarnings("unused")
    public void conver() throws Exception {
        if (docFile.exists()) {
            if (!pdfFile.exists()) {
                String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4";
                // Èç¹û´ÓÎļþÖжÁÈ¡µÄURLµØÖ·×îºóÒ»¸ö×Ö·û²»ÊÇ '\'£¬ÔòÌí¼Ó'\'
                if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
                    OpenOffice_HOME += "\\";
                }
                // Æô¶¯OpenOfficeµÄ·þÎñ
                String command = OpenOffice_HOME
                        + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
                Process pro = Runtime.getRuntime().exec(command);
 
                OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
                try {
                    connection.connect();
                    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                    converter.convert(docFile, pdfFile);
                    connection.disconnect();
                    System.out.println("dfת»»³É¹¦£¬±£´æÂ·¾¶£º" + pdfFile.getPath());
                } catch (java.net.ConnectException e) {
                    e.printStackTrace();
                    System.out.println("ת»»Ê§°Ü£¬openoffice·þÎñδÆô¶¯£¡");
                    throw e;
                } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
                    e.printStackTrace();
                    System.out.println("¶Áȡת»»Îļþʧ°Ü");
                    throw e;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }
            } else {
                System.out.println("ÎļþÒÑ´æÔÚ£¬²»ÐèҪת»»");
            }
        } else {
            System.out.println("ÐèҪת»»µÄÎĵµ²»´æÔÚ£¬ÎÞ·¨×ª»»");
        }
    }
 
    /**
     * ·µ»ØpdfÎļþ·¾¶
     * 
     * @return
     */
    public String getpdfPath() {
        if (pdfFile.exists()) {
            String tempString = pdfFile.getPath();
            tempString = tempString.replaceAll("\\\\", "/");
            return tempString;
        } else {
            return "";
        }
    }
 
    public static void main(String[] args) throws Exception {
        String fileString = "D:/upload/ppt4.ppt";
 
        docConverterUtil dcu = new docConverterUtil(fileString);
        dcu.setOutputPath("D:/upload/a");
        dcu.conver();
 
        System.out.println(dcu.getpdfPath());
 
    }
}