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官网

posted @ 2019-05-14 18:14  慕尘  阅读(8067)  评论(0编辑  收藏  举报