Springboot ppt转pdf——aspose方式
1. 下载ppt转pdf所需要的包
[网盘地址](https://pan.baidu.com/s/1V-CZ0zXcJzKofxr6qC1g8A)
提取码:86lp
2. maven添加依赖
- 在项目根目录创建lib文件夹
- 将下载的aspose-slides.jar包,放在lib目录下
- 在pom.xml 引入本地依赖包
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>19.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose.slides-19.3.jar</systemPath>
</dependency>
3. 将license.xml文件放置在resources根目录下(去水印)
4. 创建java类,编写代码
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.*;
import java.util.Date;
public class PPT2PdfAsposeUtil {
public static boolean getLicensePPT() {
boolean result = false;
InputStream is = null;
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");
is = resources[0].getInputStream();
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
*
* @param inPath ppt路径
* @param outPath pdf路径
* @return
*/
public static boolean ppt2Pdf(String inPath,String outPath){
// 验证License 去除水印
if (!getLicensePPT()) {
return false;
}
long start = new Date().getTime();
try {
FileInputStream fileInput = new FileInputStream(inPath);
Presentation pres = new Presentation(fileInput);
FileOutputStream out = new FileOutputStream(new File(outPath));
pres.save(out, SaveFormat.Pdf);
out.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
long end = new Date().getTime();
System.out.println("pdf转换成功,共耗时:" + ((end - start) / 1000.0) + "秒"); // 转化用时
return true;
}
public static void main(String[] args) {
ppt2Pdf("E:\\code\\test\\1.pptx","E:\\code\\test\\1.pdf");
}
}
5. 测试
执行main方法
public static void main(String[] args) {
ppt2Pdf("E:\\code\\test\\1.pptx","E:\\code\\test\\1.pdf");
}