Spring boot使用Aspose.Slides操作ppt轉PDF、轉圖片


最近要將ppt轉為PDF和圖片,Apache poi ,jacob都試了下

Apache poi 轉圖片亂碼,處理了,還會存在部分亂碼

jacob對系統依賴比較大,必須是windows還得安裝MS Office,如果同時安裝了WPS,還會調用WPS處理,還出現異常

因此換成了Aspose.Slides,這個是商用的,帶有水印

本文使用的是去除水印的 aspose.slides-19.3.jar( 獲取資源 提取碼:zhb8)

去除水印的方法 查看

1.創建spring boot項目

2.准備

(1)導入Aspose.Slides的jar包

(2)將license.xml,放到src/main/resources下

(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官網


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM