同时读取PPT和pptx的时候低版本会产生依赖冲突,解决了依赖冲突后又发现没了关键包导致读取失败。。。。。。。
解决方法:将两个依赖升到4.12,就行了。
真能折腾
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
读取ppt或者pptx的代码
package com.example.demobystudy.util;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class PPTXtoImageUtils {
public static void main(String[] args) {
String url =
// "https://img.bseera.com/p/t100000764/202210201120116902W100000394.ppt";
"https://img.bseera.com/p/t100000764/202210201143547531W100000394.pptx";
List<String> strings = transPPTXToPic(url,
"D:\\workPlace\\java\\BeiShi\\测试ppt转图片\\");
System.out.println(strings);
}
/**
* @Author: RedRush
* @Date: 2022/6/29 22:32
* @description: ppt/pptx 转换为图片
*/
public static List<String> transPPTXToPic(String url,String localPath) {
try {
if (url.toUpperCase().endsWith(".PPTX")) {
return transPPTXToPicByPPTX(url,localPath);
} else if (url.toUpperCase().endsWith(".PPT")) {
return transPPTToPicByPPt(url,localPath);
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList<>();
}
// PPT输出为图片 hslf解析
private static List<String> transPPTToPicByPPt(String url,String filePath) {
List<String> outFileList = new ArrayList<>();
// 生成输出
String outRoot = filePath + url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf('.')) + File.separator;
System.err.printf("图片输出路径为:%s\n", outRoot);
// 不存在则创建文件夹
mkdir(outRoot);
try {
// 读取ppt
HSLFSlideShow ppt = new HSLFSlideShow(new URL(url).openStream());
/*
* 解析PPT基本内容
* */
Dimension sheet = ppt.getPageSize();
int width = sheet.width, height = sheet.height;
List<HSLFSlide> pages = ppt.getSlides();
System.err.printf("ppt基本信息: 共%s页,尺寸: %s , %s", pages.size(), width, height);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
int i = 1;
// 逐页遍历
for (HSLFSlide slide : pages) {
// 清空画板
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, width, height));
slide.draw(graphics);
String outFile = outRoot + i++ + ".png";
outFileList.add(outFile);
// 输出为图片
File f = new File(outFile);
System.out.printf("输出图片:%s\n", f.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(f);
javax.imageio.ImageIO.write(img, "PNG", fos);
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return outFileList;
}
// PPTX输出为图片 xmls包解析
private static List<String> transPPTXToPicByPPTX(String url,String filePath) {
List<String> outFileList = new ArrayList<>();
// 生成输出
String outRoot = filePath + url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf('.')) + File.separator;
System.err.printf("图片输出路径为:%s\n", outRoot);
// 不存在则创建文件夹
mkdir(outRoot);
try {
// 读取ppt
XMLSlideShow ppt = new XMLSlideShow(new URL(url).openStream());
/*
* 解析PPT基本内容
* */
Dimension sheet = ppt.getPageSize();
int width = sheet.width, height = sheet.height;
int count = ppt.getSlides().size();
System.err.printf("ppt基本信息: 共%s页,尺寸: %s , %s", count, width, height);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
int i = 1;
// 逐页遍历
for (XSLFSlide shape : ppt.getSlides()) {
// 清空画板
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, width, height));
shape.draw(graphics);
String outFile = outRoot + i++ + ".png";
outFileList.add(outFile);
// 输出为图片
File f = new File(outFile);
System.out.printf("输出图片:%s\n", f.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(f);
javax.imageio.ImageIO.write(img, "PNG", fos);
fos.close();
}
} catch (Exception e) {
System.err.println("======ppt转换异常");
e.printStackTrace();
}
return outFileList;
}
// 生成文件夹
private static void mkdir(String path) {
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
}
}
maven查找依赖的地址:http://www.mvnrepository.com/