jdk 版本 1.8
其他相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.2.RELEASE</version>
<type>pom</type>
</dependency>
<!-- easy excel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.3</version>
</dependency>
ppt 转 png 工具,互联网上的都大同小异,仅做参考。
public static List<File> ppt2Png(File pptFile) {
IOUtils.setByteArrayMaxOverride(13392024);
List<File> pngFileList = new ArrayList<>();
long startTime = System.currentTimeMillis();
FileInputStream is = null;
HSLFSlideShow ppt = null;
try {
ZipSecureFile.setMinInflateRatio(-1.0d);
is = new FileInputStream(pptFile);
ppt = new HSLFSlideShow(is);
int idx = 1;
Dimension pageSize = ppt.getPageSize();
double image_rate = 2.0;
int imageWidth = (int) Math.floor(image_rate * pageSize.getWidth());
int imageHeight = (int) Math.floor(image_rate * pageSize.getHeight());
for (HSLFSlide slide : ppt.getSlides()) {
BufferedImage img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
// clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, imageWidth, imageHeight));
graphics.scale(image_rate, image_rate);
//防止中文乱码
for (HSLFShape shape : slide.getShapes()) {
if (shape instanceof HSLFTextShape) {
HSLFTextShape hslfTextShape = (HSLFTextShape) shape;
for (HSLFTextParagraph hslfTextParagraph : hslfTextShape) {
for (HSLFTextRun hslfTextRun : hslfTextParagraph) {
hslfTextRun.setFontFamily("宋体");
}
}
}
}
FileOutputStream out = null;
try {
slide.draw(graphics);
File pngFile = new File(pptFile.getPath().replace(".ppt", String.format("-%04d.png", idx++)));
out = new FileOutputStream(pngFile);
ImageIO.write(img, "png", out);
pngFileList.add(pngFile);
} catch (Exception e) {
log.error("ppt2Png exception", e);
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (graphics != null) {
graphics.dispose();
}
if (img != null) {
img.flush();
}
} catch (IOException e) {
log.error("ppt2Png close exception", e);
}
}
}
} catch (Exception e) {
log.error("ppt2Png exception", e);
} finally {
try {
if (is != null) {
is.close();
}
if (ppt != null) {
ppt.close();
}
} catch (Exception e) {
log.error("ppt2Png exception", e);
}
}
long endTime = System.currentTimeMillis();
log.info("ppt2Png的时间:{}", endTime - startTime);
return pngFileList;
}
2.1.3 版本中 easyexcel 中引用的 poi 包都是 3.17,会导致设置背景色失效。
解决办法,排除 easyexcel 中的 poi 依赖,重新引入 4.1.1 版本。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.3</version>
<exclusions>
<exclusion>
<artifactId>poi</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.1</version>
</dependency>