WordToPdfUtil.java
6.04 KB
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
package com.lhcredit.common.utils;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* Created by llss on 2018/9/10
*/
@Component
@Slf4j
public class WordToPdfUtil implements InitializingBean {
@Value("${tempDirectory}")
public String tempDirectoryLux;
@Value("${fileDirectory}")
private String fileDirectoryLux;
@Value("${tempDirectoryWin}")
private String tempDirectoryWin;
@Value("${fileDirectoryWin}")
private String fileDirectoryWin;
@Value("${reportTemplateWin}")
private String resourcePathWin;
@Value("${reportTemplate}")
private String resourcePathLux;
public static String tempDirectory;
public static String fileDirectory;
public static String resourcePath;
public void init(){
String os = System.getProperty("os.name");
if (StringUtils.isEmpty(WordToPdfUtil.fileDirectory)||StringUtils.isEmpty(WordToPdfUtil.tempDirectory)){
if (os.contains("Windows")){
WordToPdfUtil.fileDirectory=fileDirectoryWin;
WordToPdfUtil.tempDirectory=tempDirectoryWin;
WordToPdfUtil.resourcePath=resourcePathWin;
}else{
WordToPdfUtil.fileDirectory=fileDirectoryLux;
WordToPdfUtil.tempDirectory=tempDirectoryLux;
WordToPdfUtil.resourcePath=resourcePathLux;
}
}
}
public static boolean getLicense() {
boolean result = false;
try {
// String tempDirectory ="C:\\Users\\llss\\IdeaProjects\\dna-portals\\src\\templates";
InputStream is = new FileInputStream(WordToPdfUtil.tempDirectory + "license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static String doc2pdf(InputStream inputStream, String pdfName) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return null;
}
try {
pdfName=pdfName+System.currentTimeMillis()+".pdf";
long old = System.currentTimeMillis();
File file = new File(WordToPdfUtil.fileDirectory + pdfName); //新建一个pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document( inputStream); //Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
os.close();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
return WordToPdfUtil.fileDirectory + pdfName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String doc2pdf(String wordPath, String pdfName) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return null;
}
try {
pdfName=pdfName+System.currentTimeMillis()+".pdf";
long old = System.currentTimeMillis();
File file = new File(WordToPdfUtil.fileDirectory + pdfName); //新建一个pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document( wordPath); //Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
os.close();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
return WordToPdfUtil.fileDirectory + pdfName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void afterPropertiesSet() throws Exception {
init();
}
public static void main(String[] args) throws Exception {
doc2pdf2("D:\\word\\华为技术有限公司1718934279631.docx","11111.pdf");
}
public static String doc2pdf2(String wordPath, String pdfPath) throws Exception {
// fileDirectory="D:\\word\\";
// InputStream is = new FileInputStream("D:\\javaProject\\credit-manager-new2\\src\\main\\resources\\docTemp\\license.xml");
InputStream is = new FileInputStream(WordToPdfUtil.tempDirectory + "license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
try {
long old = System.currentTimeMillis();
File file = new File(fileDirectory + pdfPath); //新建一个pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); //Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
os.close();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
return fileDirectory + pdfPath;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String filePathChangeOs(String path){
String os = System.getProperty("os.name");
if (!os.contains("Windows")) {
path = path.replace("/\\", "/").replace("\\", "/");
}else{
path = path.replace("/", "\\").replace("\\", "\\");
}
return path;
}
}