引入所需依赖,注意poi版本,新版本不支持,最好使用和我一样的版本。
调用方式:
参数fileformat:文件名后缀,例ppt、pptx
参数str:文件绝对路径
public static void saveYulanFile(String fileformat,String str) throws Exception {
if(fileformat.equals("ppt") || fileformat.equals("pptx")){
PptToImage.createPPTImage(new FileInputStream(new File(str)),str.substring(0,str.indexOf("."))+"/");
}
}
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* @author :llf
* @date :Created in 2020-04-09 11:56
* @description:${description}
* @version: v1.0
*/
public class PptToImage {
public static void create2007PPTImage(InputStream in,String fileOutPath){
try {
XMLSlideShow xmlSlideShow=new XMLSlideShow(in);
Dimension pgsize = xmlSlideShow.getPageSize();
XSLFSlide\[\] slides = xmlSlideShow.getSlides();
FileOutputStream out = null;
for (int i = 0;i < slides.length;i++){
CTSlide rawSlide=slides\[i\].getXmlObject();
CTGroupShape gs = rawSlide.getCSld().getSpTree();
CTShape\[\] shapes = gs.getSpArray();
for (CTShape shape : shapes) {
CTTextBody tb = shape.getTxBody();
if (null == tb)
continue;
CTTextParagraph\[\] paras = tb.getPArray();
CTTextFont font=CTTextFont.Factory.parse(
"<xml-fragment xmlns:a=\\"http://schemas.openxmlformats.org/drawingml/2006/main\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:p=\\"http://schemas.openxmlformats.org/presentationml/2006/main\\">"+
"<a:rPr lang=\\"zh-CN\\" altLang=\\"en-US\\" dirty=\\"0\\" smtClean=\\"0\\"> "+
"<a:latin typeface=\\"+mj-ea\\"/> "+
"</a:rPr>"+
"</xml-fragment>");
for (CTTextParagraph textParagraph : paras) {
CTRegularTextRun\[\] textRuns = textParagraph.getRArray();
for (CTRegularTextRun textRun : textRuns) {
CTTextCharacterProperties properties=textRun.getRPr();
properties.setLatin(font);
}
}
}
BufferedImage img=new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE\_INT\_RGB);
Graphics2D graphics=img.createGraphics();
graphics.setPaint(Color.WHITE);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,pgsize.height));
slides\[i\].draw(graphics);
out = new FileOutputStream(fileOutPath+i+".png");
javax.imageio.ImageIO.write(img, "png", out);
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void create2003PPTImage(InputStream in,String fileOutPath){
try {
SlideShow slideShow=new SlideShow(in);
Dimension pgsize = slideShow.getPageSize();
Slide\[\] slides=slideShow.getSlides();
FileOutputStream out = null;
for(int i = 0;i < slides.length;i++){
TextRun\[\] rtruns = slides\[i\].getTextRuns();
for(TextRun tr: rtruns){
for (RichTextRun index : tr.getRichTextRuns()){
index.setFontName("宋体");
}
}
BufferedImage img=new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE\_INT\_RGB);
Graphics2D graphics=img.createGraphics();
graphics.setPaint(Color.WHITE);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,pgsize.height));
slides\[i\].draw(graphics);
out = new FileOutputStream(fileOutPath+i+".png");
javax.imageio.ImageIO.write(img, "png", out);
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createPPTImage(InputStream in,String fileOutPath){
try {
if(!in.markSupported()){
in=new BufferedInputStream(in);
}
if(in.markSupported()){
in=new PushbackInputStream(in,8);
}
File file = new File(fileOutPath);
if(!file.exists()){
file.mkdirs();
}
if(POIFSFileSystem.hasPOIFSHeader(in)){//2003
create2003PPTImage(in,fileOutPath);
}else if(POIXMLDocument.hasOOXMLHeader(in)){//2007
create2007PPTImage(in,fileOutPath);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
生成的图片目录和文件路径在同一位置
手机扫一扫
移动阅读更方便
你可能感兴趣的文章