最近要将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官网

Spring boot使用Aspose.Slides操作ppt转PDF、转图片的更多相关文章

  1. Spring Boot(二):数据库操作

    本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...

  2. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  3. spring boot编程思想(核心篇) pdf 下载 it教程

    资料简介:本书是<Spring Boot 编程思想>的核心篇,开篇总览Spring Boot核心特性,接着讨论自动装配(Auto-Configuration)与SpringApplicat ...

  4. Spring Boot实战之数据库操作

    上篇文章中已经通过一个简单的HelloWorld程序讲解了Spring boot的基本原理和使用.本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是Jdb ...

  5. spring boot ----> jpa连接和操作mysql数据库

    环境: centos6.8,jdk1.8.0_172,maven3.5.4,vim,spring boot 1.5.13,mysql-5.7.23 1.引入jpa起步依赖和mysql驱动jar包 &l ...

  6. spring boot通过Jedis来操作redis

    idea中新建spring boot项目,引入jedis依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> ...

  7. 使用spring boot中的JPA操作数据库

    前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...

  8. Spring Boot 使用 Dom4j XStream 操作 Xml

    Xml 现在仍然占据着比较重要的地位,比如微信接口中使用了 Xml 进行消息的定义.本章重点讨论 Xml 的新建.编辑.查找.转化,可以这么理解,本章是使用了 dom4j.xstream 也是在开发者 ...

  9. 阿里云服务器 配置 tomcat 发布spring boot项目 的具体操作 【使用公网ip】

    1.前言 spring boot 转成war包 后用tomcat发布的具体操作在我另一篇随笔有详细记载,不论是window系统还是Linux系统,tomcat的发布配置都是一样的,所以这里不具体讲这个 ...

随机推荐

  1. C# 模拟鼠标移动和点击

    我们需要用到的mouse_event函数,位于user32.dll这个库文件里面,所以我们要先声明引用. [System.Runtime.InteropServices.DllImport(" ...

  2. 代码编辑器与IDE(集成开发环境)

    编辑器就是轻量级的只用于编辑代码: nodepad++, sublime, ...... IDE就是包含很多例如调试, 编译,UI界面的功能更为完善的软件: Pycharm(python用的多), V ...

  3. 15 分钟学会使用 Git 和远程代码库

    Git是个了不起但却复杂的源代码管理系统.它能支持复杂的任务,却因此经常被认为太过复杂而不适用于简单的日常工作.让我们诚实一记吧:Git是复杂的,我们不要装作它不是.但我仍然会试图教会你用(我的)基本 ...

  4. GlusterFS集群文件系统研究

    https://blog.csdn.net/liuaigui/article/details/6284551 1.      GlusterFS概述GlusterFS是Scale-Out存储解决方案G ...

  5. LeetCode 358. Rearrange String k Distance Apart

    原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...

  6. PowerDesigner 画流程图

    原因: 以前赶时间写了n长一个类,现在又增加新需求了,but以前怎么写的忘了,虽然注释都有,一个一个方法的看很累,准备把它用面向对象改造一下,不知道时间够不,先试一试在说.之前设计数据库用的Power ...

  7. rollup node.js 打包工具

    最近在做一个提供给浏览器和node同时使用的js的url模板工具类,在用什么打包工具上纠结了一段时间,正好有一天在知乎上看到了关于rollup的介绍,在自己试了试之后,就决定用rollup.js来打包 ...

  8. Hdu 5093 Battle Ship

    每个海面要么放要么不放,因此可以用二分图匹配, 考虑把同一行内的能互相看到的点放到一个行块里,同一列内能看到的点放到一个列块里,然后每一个行块都可以和该行块里所有海面的列块连边,选了这个行块,就必须选 ...

  9. GoCN每日新闻(2019-10-07)

    GoCN每日新闻(2019-10-07) 国庆专辑:GopherChina 祝大家国庆节快乐(假期最后一天)   GoCN每日新闻(2019-10-07)   1. Go 不好的点:JSON 解析的探 ...

  10. CSS — BEM 命名规范

    推荐阅读: https://juejin.im/post/5b925e616fb9a05cdd2ce70d 1 什么是 BEM 命名规范 Bem 是块(block).元素(element).修饰符(m ...