POI开源项目-PPT2PNG转换测试

时间:2022-02-22 15:51:26

第一次学使用软件去读取Office类文件。

今天百度了一下,发现Apache也是有这样的开源项目的,POI提供下列支持:

POI开源项目-PPT2PNG转换测试

测试了一下HSLF对PPT的读操作,并将PPT转化为图片保存到指定目录:

使用之前必须将包引入:

 POI开源项目-PPT2PNG转换测试

关联包:

POI开源项目-PPT2PNG转换测试

测试主函数如下:

设置输入ppt文件、输出路径、以及生成图片规模大小。

 package edu.office;

 import java.util.List;

 public class TestTransfor {

     /**
* 测试ppt转换
*
* **/
public static void main(String[] args) {
PPTReader reader = new PPTReader();
reader.setPPT_file_path("D:/1.ppt");
reader.setOutput_path("D:/ppt/");
//控制图片尺寸 1倍 2倍 3倍
reader.setScale(1); List<String> images = reader.ppt2png();
for(String image : images){
System.out.println(image);
}
} }

PPT转化png类如下:

 package edu.office;

 import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import javax.imageio.ImageIO; import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow; public class PPTReader{ //ppt输入文件路径
private String ppt_file_path;
//ppt2png保存png图片的路径
private String output_path;
//scale是图片是原来的size几倍
private int scale = 1; public String getPpt_file_path() {
return ppt_file_path;
}
public void setPPT_file_path(String ppt_file_path) {
this.ppt_file_path = ppt_file_path;
}
public int getScale() {
return scale;
}
public void setScale(int scale) {
this.scale = scale;
}
public String getOutput_path() {
return output_path;
}
public void setOutput_path(String output_path) {
this.output_path = output_path;
} /**ppt转png
*
* @return null 失败 list<String> 图片名称列表
* **/
public List<String> ppt2png(){
//int slidenum = 2;
List<String> images = new ArrayList<String>();
try {
FileInputStream is;
is = new FileInputStream(ppt_file_path);
SlideShow ppt = new SlideShow(is);
is.close(); Dimension pgsize = ppt.getPageSize(); int width = (int)(pgsize.width*scale);
int height = (int)(pgsize.height*scale); Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) { //if (slidenum != -1 && slidenum != (i+1)) continue;
//获取每页ppt标题
String title = slide[i].getTitle();
//System.out.println("Rendering slide "+slide[i].getSlideNumber() + (title == null ? "" : ": " + title)); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
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); graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, width, height)); graphics.scale((double)width/pgsize.width, (double)height/pgsize.height); slide[i].draw(graphics);
//对每一张图片进行命名
String fname = output_path + "ppt-" + (i+1) + ".png";
images.add("ppt-" + (i+1) + ".png");
File file = new File(output_path);
if(!file.exists()){
file.mkdirs();
}
FileOutputStream out = new FileOutputStream(fname);
ImageIO.write(img, "png", out);
out.close();
}
return images;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}