(1)新建license.xml文件,路径放在resources下
(2)license.xml文件里面内容如下 <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>
(3)下载相关依赖jar包,只展示一个下载图片,j只展示了pdf下载官方地址Aspose.PDF 20.11 | Process Acrobat PDF Files via Java API
(4)相关依赖
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>19.5-jdk</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-19.5jdk.jar</systemPath> </dependency> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-cells</artifactId> <version>8.5.2</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-8.5.2.jar</systemPath> </dependency> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-slides</artifactId> <version>15.9.0</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/aspose.slides-15.9.0.jar</systemPath> </dependency>
<dependency> <groupId>com.aspose-pdf</groupId> <artifactId>aspose-pdf</artifactId> <version>20.11</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/aspose-pdf-20.11.jar</systemPath> </dependency>
(3)代码实现pdf转png
public static void pdfChangePng(InputStream input, OutputStream output) { // 验证License if (!getLicenseOfPDF()) { throw new RuntimeException("许可验证失败"); } try { com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(input); pdf.save(output, SaveFormat.PNG); output.flush(); output.close(); input.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("pdf转png失败"); } }
public static boolean getLicenseOfPDF() { boolean result = false; try { InputStream is = PictureUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.pdf.License aposeLic = new com.aspose.pdf.License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
(4)ppt转png
public static boolean getLicenseOfPPT() { boolean result = false; try { InputStream is = PictureUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.slides.License aposeLic = new com.aspose.slides.License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
public static void pptChangePng(InputStream input, OutputStream output) { // 验证License if (!getLicenseOfPPT()) { throw new RuntimeException("许可验证失败"); } try { Presentation pres = new Presentation(input); pres.save(output, SaveFormat.PNG); output.flush(); output.close(); input.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("ppt转png失败"); }
}
(5)其他类型不想写了,doc转png,还有excel转png,不会写的私我
(6)有些朋友可能需要把其他文件转成pdf,只需要,以ppt举例,改成下面这样就可以
com.aspose.slides.Presentation pres = new com.aspose.slides.Presentation(input); pres.save(output, com.aspose.slides.SaveFormat.Pdf);
(7)需要将其他文件转pdf的朋友注意啦,转成的文件出现乱码的情况,需要在服务器上装字体库,不然转成的文件字体乱码,切记装字体库。