whycxzp
2022-09-07 a7ca0f9df58733e6c8822fbf29962352d668871c
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
package com.whyc.util;
 
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class Word2PdfAsposeUtil {
 
    /**去除水印专用*/
    /*public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }*/
 
    public static boolean doc2pdf(String inPath, String outPath) {
        /*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return false;
        }*/
        FileOutputStream os = null;
        try {
            File file = new File(outPath); // 新建一个空白pdf文档
            os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
}