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
package com.yiboshi.science.utils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.List;
public class WordToPdfConverter {
private static final Logger logger = LoggerFactory.getLogger(WordToPdfConverter.class);
/**
* 将Word文档转换为PDF
* @param wordPath Word文档路径
* @param pdfPath 输出PDF路径
* @return 转换是否成功
*/
public static boolean convertToPdf(String wordPath, String pdfPath) {
// 检查文件是否存在
File wordFile = new File(wordPath);
if (!wordFile.exists()) {
logger.error("Word文件不存在: {}", wordPath);
return false;
}
// 检查输出目录是否存在
File pdfFile = new File(pdfPath);
File parentDir = pdfFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
try {
if (wordPath.toLowerCase().endsWith(".docx")) {
return convertDocxToPdf(wordPath, pdfPath);
} else if (wordPath.toLowerCase().endsWith(".doc")) {
return convertDocToPdf(wordPath, pdfPath);
} else {
logger.error("不支持的文件格式: {}", wordPath);
return false;
}
} catch (Exception e) {
logger.error("Word转PDF失败", e);
return false;
}
}
/**
* 将Word文档转换为PDF(字节数组版本)
* @param wordBytes Word文档的字节数组
* @param pdfPath 输出PDF路径
* @return 转换是否成功
*/
public static boolean convertToPdf(byte[] wordBytes, String pdfPath) {
if (wordBytes == null || wordBytes.length == 0) {
logger.error("Word文档字节数组为空");
return false;
}
// 检查输出目录是否存在
File pdfFile = new File(pdfPath);
File parentDir = pdfFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
try {
// 创建临时文件
File tempFile = File.createTempFile("temp", ".docx");
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
fos.write(wordBytes);
}
// 转换文件
boolean result = convertToPdf(tempFile.getAbsolutePath(), pdfPath);
// 删除临时文件
tempFile.delete();
return result;
} catch (Exception e) {
logger.error("Word转PDF失败", e);
return false;
}
}
/**
* 转换DOCX文件到PDF
*/
private static boolean convertDocxToPdf(String docxPath, String pdfPath) throws IOException {
try (FileInputStream fis = new FileInputStream(docxPath);
XWPFDocument document = new XWPFDocument(fis);
PDDocument pdfDocument = new PDDocument()) {
// 获取文档内容
List<XWPFParagraph> paragraphs = document.getParagraphs();
// 创建PDF页面
PDPage page = new PDPage();
pdfDocument.addPage(page);
// 加载字体
PDType0Font font = PDType0Font.load(pdfDocument,
WordToPdfConverter.class.getResourceAsStream("/fonts/simsun.ttc"));
// 创建内容流
try (PDPageContentStream contentStream =
new PDPageContentStream(pdfDocument, page)) {
float y = page.getMediaBox().getHeight() - 50;
// 写入段落
for (XWPFParagraph para : paragraphs) {
String text = para.getText();
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(50, y);
contentStream.showText(text);
contentStream.endText();
y -= 15;
}
}
// 保存PDF
pdfDocument.save(pdfPath);
return true;
}
}
/**
* 转换DOC文件到PDF
*/
private static boolean convertDocToPdf(String docPath, String pdfPath) throws IOException {
try (FileInputStream fis = new FileInputStream(docPath);
HWPFDocument document = new HWPFDocument(fis);
PDDocument pdfDocument = new PDDocument()) {
// 获取文档内容
String text = document.getDocumentText();
// 创建PDF页面
PDPage page = new PDPage();
pdfDocument.addPage(page);
// 加载字体
PDType0Font font = PDType0Font.load(pdfDocument,
WordToPdfConverter.class.getResourceAsStream("/fonts/simsun.ttc"));
// 创建内容流
try (PDPageContentStream contentStream =
new PDPageContentStream(pdfDocument, page)) {
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(50, page.getMediaBox().getHeight() - 50);
contentStream.showText(text);
contentStream.endText();
}
// 保存PDF
pdfDocument.save(pdfPath);
return true;
}
}
/**
* 检查文件是否为Word文档
*/
public static boolean isWordFile(String fileName) {
if (fileName == null || fileName.trim().isEmpty()) {
return false;
}
String lowerFileName = fileName.toLowerCase();
return lowerFileName.endsWith(".doc") || lowerFileName.endsWith(".docx");
}
/**
* 从远程URL下载Word文档并转换为PDF
* @param wordUrl Word文档的URL地址
* @param pdfPath 输出PDF路径
* @return 转换是否成功
*/
public static boolean convertUrlToPdf(String wordUrl, String pdfPath) {
if (wordUrl == null || wordUrl.trim().isEmpty()) {
logger.error("Word文档URL为空");
return false;
}
InputStream inputStream = null;
try {
// 创建URL连接
java.net.URL url = new java.net.URL(wordUrl);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
// 检查响应码
if (conn.getResponseCode() != java.net.HttpURLConnection.HTTP_OK) {
logger.error("下载Word文档失败,HTTP响应码: {}", conn.getResponseCode());
return false;
}
// 读取文件内容
inputStream = conn.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// 转换为PDF
byte[] wordBytes = outputStream.toByteArray();
return convertToPdf(wordBytes, pdfPath);
} catch (Exception e) {
logger.error("从URL下载Word文档失败: {}", wordUrl, e);
return false;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error("关闭输入流失败", e);
}
}
}
}
}