shichongfu
2023-06-07 c28e9990179f5573ea94f41130fcec8c64f7902a
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.product.file.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.RoundingMode;
import java.text.DecimalFormat;
 
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.product.core.exception.BaseException;
import com.product.core.spring.context.SpringMVCContextHolder;
 
public class AsposeUtil {
    //是否初始了license
    private static boolean isInitLicense=false;
    /**
     * 获取license
     * @return
     */
    public static boolean getLicense(int type)throws BaseException {
        //已经初始过,不再初始
        if(isInitLicense) {
            return true;
        }
        boolean result = true;
        InputStream is = null;
        try {
            is =  AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
            com.aspose.cells.License excel = new com.aspose.cells.License();
            excel.setLicense(is);
            
            is =  AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
            com.aspose.words.License word = new com.aspose.words.License();
            word.setLicense(is);
            
            is =  AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
            com.aspose.slides.License ppt = new com.aspose.slides.License();
            ppt.setLicense(is);
 
            isInitLicense=true;
        } catch (Exception e) {
            result=false;
            throw new BaseException(e);
        }finally{
            if(is!=null) {
                try {
                    is.close();
                }catch(IOException e) {
                    throw new BaseException(e);
                }
            }
        }
        return result;
    }
    /**
     * 
     * @param officePath
     * @param OutPutPath
     * @throws BaseException
     */
    public static void Excel2Pdf(String officePath,String OutPutPath)throws BaseException {
        // 验证License
        if (!getLicense(1)) {
            return;
        }
        FileOutputStream fileOS=null;
        try {
            File file = new File(OutPutPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            Workbook wb = new Workbook(officePath);// 原始excel路径
            File pdfFile = new File(OutPutPath);// 输出路径
            fileOS = new FileOutputStream(pdfFile);
            //wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
            wb.save(fileOS, pdfSaveOptions);
        } catch (Exception e) {
            throw new BaseException(e);
        }finally{
            if(fileOS!=null) {
                try {
                    fileOS.flush();
                    fileOS.close();
                }catch(IOException e) {
                    throw new BaseException(e);
                }
            }
        }
    }
    /**
     * 
     * @param officePath
     * @param OutPutPath
     * @throws BaseException
     */
    public static void Word2Pdf(String officePath,String OutPutPath) throws BaseException {
        // 验证License
        if (!getLicense(2)) {
            return;
        }
        FileOutputStream fileOS=null;
        try {
            File file = new File(OutPutPath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            Document doc = new Document(officePath);// 原始word路径
            File pdfFile = new File(OutPutPath);// 输出路径
            fileOS = new FileOutputStream(pdfFile);
            doc.save(fileOS, com.aspose.words.SaveFormat.PDF);
        } catch (Exception e) {
            throw new BaseException(e);
        }finally{
            if(fileOS!=null) {
                try {
                    fileOS.flush();
                    fileOS.close();
                }catch(IOException e) {
                    throw new BaseException(e);
                }
            }
        }
    }
    /**
     * 
     * @param officePath
     * @param OutPutPath
     * @throws BaseException
     */
    public static void PPT2Pdf(String officePath,String OutPutPath)throws BaseException {
        // 验证License
        if (!getLicense(3)) {
            return;
        }
        FileOutputStream fileOS=null;
        try {
            File PathFile = new File(OutPutPath);
            if (!PathFile.exists()) {
                PathFile.mkdirs();
            }
            InputStream slides = new FileInputStream(new File(officePath));// 原始ppt路径
            Presentation pres = new Presentation(slides);
            File file = new File(OutPutPath);// 输出pdf路径
            fileOS = new FileOutputStream(file);
            pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
            fileOS.flush();
            fileOS.close();
        } catch (Exception e) {
            throw new BaseException(e);
        }finally{
            if(fileOS!=null) {
                try {
                    fileOS.flush();
                    fileOS.close();
                }catch(IOException e) {
                    throw new BaseException(e);
                }
            }
        }
    }
    /**
     * 文档转换
     * @param officePath
     * @param outFile
     * @return
     * @throws BaseException
     */
    public static String OfficeToPdf(String officePath,String outFile,String officeType )throws BaseException {
 
        SpringMVCContextHolder.getSystemLogger().info("The file of office type:"+officePath);
        SpringMVCContextHolder.getSystemLogger().info("The file of PDF type:"+outFile);
        File file = new File(officePath);
        //判断当前office文件是否已经转为PDF,如果已转为PDF就不需要再次转换。
        if (file.exists()&& !"pdf".equals(officeType)) {
            double bytes = file.length();
            double kilobytes = (bytes / 1024);
            double megabytes = (kilobytes / 1024);
 
            DecimalFormat df = new DecimalFormat("0.00");
            df.setRoundingMode(RoundingMode.HALF_UP);
            String MB = df.format(megabytes);
            Double Size = Double.parseDouble(MB);
//            if(Size>10){
            SpringMVCContextHolder.getSystemLogger().info("The file size:"+Size+"MB");
//            }
            //"doc", "docx", "xls","xlsx", "ppt", "pptx"
            if(officeType.equals("doc")||officeType.equals("docx")){
                Word2Pdf(officePath,outFile);
            }else if(officeType.equals("xls")||officeType.equals("xlsx")){
                Excel2Pdf(officePath,outFile);
            }else if(officeType.equals("ppt")||officeType.equals("pptx")){
                PPT2Pdf(officePath,outFile);
            }else{
                SpringMVCContextHolder.getSystemLogger().info("File types that cannot be converted:"+officeType);
            }
        } else {
            SpringMVCContextHolder.getSystemLogger().info("File Not Exist");
        }
        return outFile;
    }
    /**
     * 文档转换
     * @param officePath
     * @return
     */
    public static String OfficeToPdf(String officePath) {
 
        //G:/product/WebApp/fwis_develop/com/is/flywings/oa/attchfile/1000000000/i0002/101951.docx⌒101951.docx⌒feiyu.docx
        String[] split = officePath.split("⌒");
        int lastIndex = split[0].lastIndexOf(".");
        int lastNameIndex = split[0].lastIndexOf("/");
 
        String officeType = split[0].substring(lastIndex+1);
        String officeName = split[0].substring(lastNameIndex+1,lastIndex)+".pdf";
        String OutPutPath = split[0].substring(0,lastNameIndex+1)+"office/";
        System.out.println("输出目录:"+OutPutPath);
        File file = new File(split[0]);
        File pdfFile = new File(OutPutPath+officeName);
        //判断当前office文件是否已经转为PDF,如果已转为PDF就不需要再次转换。
        if(pdfFile.exists()){
            return OutPutPath+officeName;
        }
 
        if (file.exists()) {
 
            double bytes = file.length();
            double kilobytes = (bytes / 1024);
            double megabytes = (kilobytes / 1024);
 
            DecimalFormat df = new DecimalFormat("0.00");
            df.setRoundingMode(RoundingMode.HALF_UP);
            String MB = df.format(megabytes);
            Double Size = Double.parseDouble(MB);
            if(Size>10){
                return Size+"MB";
            }
            //"doc", "docx", "xls","xlsx", "ppt", "pptx"
            try {
                if(officeType.equals("doc")||officeType.equals("docx")){
                    Word2Pdf(split[0],OutPutPath+"/"+officeName);
                }else if(officeType.equals("xls")||officeType.equals("xlsx")){
                    Excel2Pdf(split[0],OutPutPath+"/"+officeName);
                }else if(officeType.equals("ppt")||officeType.equals("pptx")){
                    PPT2Pdf(split[0],OutPutPath+"/"+officeName);
                }else{
                    System.out.println("无法识别该文件!");
                    return "Error";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            return "NotExists";
        }
        return OutPutPath+officeName;
    }
    
    public static void main(String args[]) {
        String file="E:/work/沄崃/FE6.6产品报价工具-新 - 副本.xlsx";
        AsposeUtil.OfficeToPdf(file);
    }
}