最近要將ppt轉爲PDF和圖片,Apache poi ,jacob都試了下html
Apache poi 轉圖片亂碼,處理了,還會存在部分亂碼java
jacob對系統依賴比較大,必須是windows還得安裝MS Office,若是同時安裝了WPS,還會調用WPS處理,還出現異常web
所以換成了Aspose.Slides,這個是商用的,帶有水印spring
本文使用的是去除水印的 aspose.slides-19.3.jar( 獲取資源 提取碼:zhb8)windows
去除水印的方法 查看api
1.建立spring boot項目app
2.準備ide
(1)導入Aspose.Slides的jar包學習
(2)將license.xml,放到src/main/resources下spa
(3)修改pom.xml
<dependency> <groupId>aspose.slides</groupId> <artifactId>slides</artifactId> <version>19.3</version> <scope>system</scope> <systemPath>${basedir}/lib/aspose.slides-19.3.jar</systemPath> </dependency>
3.轉PDF
目標文件data/CoreThink.pptx
pdf保存data/CoreThink.pdf
package com.slides.ppt.controller; import com.aspose.slides.License; import com.aspose.slides.Presentation; import com.aspose.slides.SaveFormat; import org.springframework.web.bind.annotation.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; @RestController @RequestMapping("/api") public class TestOperation { private static InputStream license; /** * 獲取license * * @return */ public static boolean getLicense() { boolean result = false; license = TestOperation.class.getClassLoader().getResourceAsStream("license.xml"); if (license != null) { License aposeLic = new License(); aposeLic.setLicense(license); result = true; } return result; } /** * 轉PDF * * @return */ @PostMapping("/convertPDF") public String convertPDF() { // 驗證License if (!getLicense()) { return "驗證License失敗"; } try { FileInputStream fileInput = new FileInputStream("data/CoreThink.pptx"); Presentation pres = new Presentation(fileInput); FileOutputStream out = new FileOutputStream(new File("data/CoreThink.pdf")); pres.save(out, SaveFormat.Pdf); out.close(); } catch (Exception e) { return e.getMessage(); } return "轉換成功"; } }
4.轉圖片
目標文件data/CoreThink.pptx
圖片保存路徑爲 文件名_JPG即CoreThink_JPG
package com.slides.ppt.controller; import com.aspose.slides.ISlide; import com.aspose.slides.License; import com.aspose.slides.Presentation; import org.springframework.web.bind.annotation.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; @RestController @RequestMapping("/api") public class TestOperation { private static InputStream license; /** * 獲取license * * @return */ public static boolean getLicense() { boolean result = false; license = TestOperation.class.getClassLoader().getResourceAsStream("license.xml"); if (license != null) { License aposeLic = new License(); aposeLic.setLicense(license); result = true; } return result; } /** * 轉Image * * @return */ @PostMapping("/convertImage") public String convertImage() { // 驗證License if (!getLicense()) { return "驗證License失敗"; } String fileName = "data/CoreThink.pptx"; File file = new File(fileName); if (!file.exists()) { return "轉換文件不存在"; } String filePath = file.getParent()+File.separator; String dest = filePath + getFileNameNoEx(file.getName())+"_JPG"; File destPath = new File(dest); if (!destPath.exists()) { destPath.mkdir(); } try { FileInputStream fileInput = new FileInputStream(fileName); Presentation pres = new Presentation(fileInput); int i; for (i = 0; i < pres.getSlides().size(); i++) { ISlide slide = pres.getSlides().get_Item(i); int height = (int)(pres.getSlideSize().getSize().getHeight()-150); int width = (int)(pres.getSlideSize().getSize().getWidth()-150); BufferedImage image = slide.getThumbnail(new java.awt.Dimension(width, height)); //每一頁輸出一張圖片 File outImage = new File(dest+File.separator + (i+1) + ".JPG"); ImageIO.write(image, "JPG", outImage); } } catch (Exception e) { return e.getMessage(); } return "轉換成功"; } /** * 獲取文件名,去除擴展名的 * * @param filename * @return */ private String getFileNameNoEx(String filename) { if ((filename != null) && (filename.length() > 0)) { int dot = filename.lastIndexOf('.'); if ((dot > -1) && (dot < (filename.length()))) { return filename.substring(0, dot); } } return filename; } }
說明:
若是沒有驗證License,輸出的會帶水印的,所以要保證 license.xml 能讀取成功,並作驗證
注意:
資源文件只容許學習使用,不得用於商業用途,請購買受權正版 aspose官網