派生自 projectDept/qhighschool

胡仁荣
2023-08-04 2174b22bbbb45284765a23b8189df59583c65d29
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());
 
    }
}