复制代码
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352/**************************************************** * 创建人: @author hechengcheng * 创建时间: 2020年12月18日/上午9:42:13 * 项目名称: WaterMarkProject * 文件名称: PdfConvertUtil.java * 文件描述: @Description: TODO(用一句话描述该文件做什么) * * All rights Reserved, Designed By 投资交易团队 * @Copyright:2016-2020 * ********************************************************/ package com.win.dfas.file.util; import com.aspose.cells.PdfSaveOptions; import com.aspose.cells.Workbook; import com.aspose.words.SaveFormat; import com.itextpdf.text.Document; import com.itextpdf.text.Image; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import org.apache.poi.hslf.usermodel.*; import org.apache.poi.xslf.usermodel.*; import java.awt.*; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.*; import java.util.List; /** * 包名称: com.csloser.topdf * 类名称:PdfConvertUtil * 类描述:转为为PDF工具类 * 创建人:@author hechengcheng * 创建时间:2020年12月18日/上午9:42:13 * */ public final class PdfConvertUtil { /** * @param inputStream 源文件输入流 * @param outputStream pdf文件输出流 * @return * @throws * @Title: imgToPdf * @Description: 图片转换为PDF文件 * @return: boolean * @author: hechengcheng * @Date: 2020年12月18日/上午9:44:34 */ public static boolean imgToPdf(InputStream inputStream, OutputStream outputStream) { Document document = null; try { // 创建文档 document = new Document(PageSize.A4, 20, 20, 20, 20); PdfWriter.getInstance(document, outputStream); document.open(); document.newPage(); // 将文件流转换为字节流,便于格式转换 BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int length = 0; while (-1 != (length = bufferedInputStream.read(bytes))) { byteArrayOutputStream.write(bytes, 0, length); } Image image = Image.getInstance(byteArrayOutputStream.toByteArray()); float height = image.getHeight(); float width = image.getWidth(); float percent = 0.0f; if (height > width) { percent = PageSize.A4.getHeight() / height * 100; } else { percent = PageSize.A4.getWidth() / width * 100; } image.setAlignment(Image.MIDDLE); image.scalePercent(percent); document.add(image); } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (document != null) { document.close(); } } catch (Exception e) { e.printStackTrace(); } } return true; } /** * @param inputStream 源文件输入流 * @param outputStream pdf文件输出流 * @return * @throws * @Title: wordToPdf * @Description: word转换为PDF文件 * @return: boolean * @author: hechengcheng * @Date: 2020年12月18日/上午9:44:34 */ public static boolean wordTopdfByAspose(InputStream inputStream, OutputStream outputStream) { // 验证License 若不验证则转化出的pdf文档会有水印产生 if (!getWordLicense()) { return false; } try { // Address是将要被转化的word文档 com.aspose.words.Document doc = new com.aspose.words.Document(inputStream); // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换 doc.save(outputStream, SaveFormat.PDF); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (outputStream != null) { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } public static boolean getWordLicense() { boolean result = false; try { String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>"; ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes()); com.aspose.words.License aposeLic = new com.aspose.words.License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public static boolean getExeclLicense() { boolean result = false; try { String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>"; ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes()); com.aspose.cells.License aposeLic = new com.aspose.cells.License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * @Title: pptToPdf * @Description: ppt格式转换pdf * @param inputStream * @param outputStream * @return: * @throws * @author: zhouyangyang * @Date: 2021/4/2 9:49 */ public static boolean pptToPdf(InputStream inputStream, OutputStream outputStream) { Document document = null; HSLFSlideShow hslfSlideShow = null; PdfWriter pdfWriter = null; try { hslfSlideShow = new HSLFSlideShow(inputStream); Dimension dimension = hslfSlideShow.getPageSize(); document = new Document(); pdfWriter = PdfWriter.getInstance(document, outputStream); document.open(); PdfPTable pdfPTable = new PdfPTable(1); List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides(); for (int i=0; i < hslfSlideList.size(); i++) { HSLFSlide hslfSlide = hslfSlideList.get(i); // 设置字体, 解决中文乱码 for (HSLFShape shape : hslfSlide.getShapes()) { HSLFTextShape textShape = (HSLFTextShape) shape; for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) { for (HSLFTextRun textRun : textParagraph.getTextRuns()) { textRun.setFontFamily("宋体"); } } } BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D graphics2d = bufferedImage.createGraphics(); graphics2d.setPaint(Color.white); graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12)); hslfSlide.draw(graphics2d); graphics2d.dispose(); Image image = Image.getInstance(bufferedImage, null); image.scalePercent(50f); // 写入单元格 pdfPTable.addCell(new PdfPCell(image, true)); document.add(image); } } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (document != null) { document.close(); } if (pdfWriter != null) { pdfWriter.close(); } } catch (Exception e) { e.printStackTrace(); } } return true; } /** * @param inputStream 源文件输入流 * @param outputStream pdf文件输出流 * @return * @throws * @Title: pptToPdf * @Description: PPT转为PDF文件 * @return: boolean * @author: hechengcheng * @Date: 2020年12月18日/下午3:17:28 */ public static boolean pptxToPdf(InputStream inputStream, OutputStream outputStream) { Document document = null; XMLSlideShow slideShow = null; PdfWriter pdfWriter = null; try { slideShow = new XMLSlideShow(inputStream); Dimension dimension = slideShow.getPageSize(); document = new Document(); pdfWriter = PdfWriter.getInstance(document, outputStream); document.open(); PdfPTable pdfPTable = new PdfPTable(1); List<XSLFSlide> slideList = slideShow.getSlides(); for (int i = 0, row = slideList.size(); i < row; i++) { XSLFSlide slide = slideList.get(i); // 设置字体, 解决中文乱码 for (XSLFShape shape : slide.getShapes()) { XSLFTextShape textShape = (XSLFTextShape) shape; for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) { for (XSLFTextRun textRun : textParagraph.getTextRuns()) { textRun.setFontFamily("宋体"); } } } BufferedImage bufferedImage = new BufferedImage((int) dimension.getWidth(), (int) dimension.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D graphics2d = bufferedImage.createGraphics(); graphics2d.setPaint(Color.white); graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12)); slide.draw(graphics2d); graphics2d.dispose(); Image image = Image.getInstance(bufferedImage, null); image.scalePercent(50f); // 写入单元格 pdfPTable.addCell(new PdfPCell(image, true)); document.add(image); } } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (document != null) { document.close(); } if (pdfWriter != null) { pdfWriter.close(); } } catch (Exception e) { e.printStackTrace(); } } return true; } /** * @Title: excelToPdf * @Description: excel转PDf * @param inputStream 源文件输出流 * @param outputStream PDF文件输出流 * @return: boolean * @throws * @author: pengyangxuan * @Date: 2021/04/02/13:52 */ public static boolean excelToPdf(InputStream inputStream, OutputStream outputStream) { // 验证License 若不验证则转化出的pdf文档会有水印产生 if (!getExeclLicense()) { return false; } try { Workbook wb = new Workbook(inputStream);// 原始excel路径 PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.setOnePagePerSheet(false); int[] autoDrawSheets={3}; //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。 autoDraw(wb,autoDrawSheets); int[] showSheets={0}; //隐藏workbook中不需要的sheet页。 printSheetPage(wb,showSheets); wb.save(outputStream, pdfSaveOptions); outputStream.flush(); outputStream.close(); System.out.println("完毕"); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return true; } /** * 设置打印的sheet 自动拉伸比例 * @param wb * @param page 自动拉伸的页的sheet数组 */ public static void autoDraw(Workbook wb,int[] page){ if(null!=page&&page.length>0){ for (int i = 0; i < page.length; i++) { wb.getWorksheets().get(i).getHorizontalPageBreaks().clear(); wb.getWorksheets().get(i).getVerticalPageBreaks().clear(); } } } /** * 隐藏workbook中不需要的sheet页。 * * @param wb * @param page 显示页的sheet数组 */ public static void printSheetPage(Workbook wb, int[] page) { for (int i = 1; i < wb.getWorksheets().getCount(); i++) { wb.getWorksheets().get(i).setVisible(false); } if (null == page || page.length == 0) { wb.getWorksheets().get(0).setVisible(true); } else { for (int i = 0; i < page.length; i++) { wb.getWorksheets().get(i).setVisible(true); } } } }
最后
以上就是高贵日记本最近收集整理的关于文件格式转换工具的全部内容,更多相关文件格式转换工具内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复