引入所需依赖,注意poi版本,新版本不支持,最好使用和我一样的版本。

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.10-FINAL</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.10-FINAL</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml-schemas</artifactId>
   <version>3.10-FINAL</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-scratchpad</artifactId>
   <version>3.10-FINAL</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.github.virtuald/curvesapi -->
<dependency>
   <groupId>com.github.virtuald</groupId>
   <artifactId>curvesapi</artifactId>
   <version>1.06</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
<dependency>
   <groupId>org.apache.xmlbeans</groupId>
   <artifactId>xmlbeans</artifactId>
   <version>3.1.0</version>
</dependency>

调用方式:
   参数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();
        }
    }
}

生成的图片目录和文件路径在同一位置

ppt和pptx转图片完整代码,解决2003版和2007版中文乱码问题的更多相关文章

  1. spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type

    问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json ...

  2. 解决get方法传递URL参数中文乱码问题

    [转]解决get方法传递URL参数中文乱码问题 来自:http://www.javaeye.com/topic/483158 应用一:解决tomcat下中文乱码问题(先来个简单的) 在tomcat下, ...

  3. 解决@ResponseBody注解返回的json中文乱码问题

    1. 简介 主要解决@ResponseBody注解返回的json中文乱码问题. 2.解决方案 2.1mvc加上注解(推荐此方法) 在mvc配置文件中假如下面配置(写在 <mvc:annotati ...

  4. 解决python语言在cmd下中文乱码的问题

    解决python语言在cmd下中文乱码的问题: a = "再见!"print (a.decode('utf-8').encode('gbk')) #解决在cmd下中文乱码的问题

  5. 分布式监控系统Zabbix-3.0.3-完整安装记录(4)-解决zabbix监控图中出现中文乱码问题

    之前部署了Zabbix-3.0.3监控系统,在安装数据库时已经将zabbix库设置了utf-8字符. 首先确定zabbix开启了中文支持功能:登录到zabbix服务器的数据目录下(前面部署的zabbi ...

  6. [转]解决get方法传递URL参数中文乱码问题

    来自:http://www.javaeye.com/topic/483158 应用一:解决tomcat下中文乱码问题(先来个简单的) 在tomcat下,我们通常这样来解决中文乱码问题: 过滤器代码: ...

  7. 解决SpringMvc后台接收json数据中文乱码问题

    原因分析 使用ajax从前台页面传输数据到后台controller控制器的时候,出现中文乱码 其实乱码问题出现的原因,就是由于默认的tomcat配置,接收请求是以ISO-8859-1来转码,导致中文出 ...

  8. 解决servlet中get方式中中文乱码问题(二):装饰者模式使用

    注意,这里是针对Tomcat容器中get方式提交的servlet中获得参数,参数中有中文的时候乱码的问题: 之前我已经讲过,Tomcat8.0及以上URIEncoding都是utf-8的默认编码,不会 ...

  9. QT笔记之解决QT5.2.0和VS2012中文乱码 以及在Qt Creator中文报错

    转载:http://bbs.csdn.net/topics/390750169 VS2012 中文乱码 1.方法一: 包含头文件 #include <QTextCodec> ....... ...

随机推荐

  1. 基于 HTML5 WebGL 的 智慧楼宇能源监控系统

    前言 21世纪,在能源危机和全球气候变暖的压力下,太阳能等可再生能源越来越受到关注,其中光伏建筑一体化逐渐成为绿色发展方式和生活方式,加强节能降耗,支持低碳产业和新能源.可再生能源发展,也已经成为国家 ...

  2. # Unity 游戏框架搭建 2019 (十六、十七) localPosition 简化与Transform 重置

    在上一篇我们收集了一个 屏幕分辨率检测的一个小工具.今天呢再往下接着探索. 问题 我们今天在接着探索.不管是写 UI 还是写 GamePlay,多多少少都需要操作 Transform. 而在笔者刚接触 ...

  3. python分布式接口,参数化实战二

    1,先看一下接口测试用例 2,文件1:写get和post模板 import requestsclass PostGetModels: def isMethod(self,url,data,method ...

  4. C# Threading.Timer 为什么一会儿自己停了

    这两天做一个socket通信的Demo,用定时器启动client端去连接server端,出现一个状况,连接几次后定时器就停了. 下面就是会造成终止的代码: public class Client { ...

  5. 树莓派 Raspberry PI基础

    树莓派 Raspberry PI基础 官网网址:https://www.raspberrypi.org 下载地址:https://www.raspberrypi.org/downloads/ 官方系统 ...

  6. flex布局你真的搞懂了吗?通俗简洁,小白勿入~

    flex布局 用以代替浮动的布局手段: 必须先把一个元素设置为弹性容器://display:flex: 一个元素可以同时是弹性容器和弹性元素; 设为flex布局以后,子元素的float.clear和v ...

  7. 1012 The Best Rank (25 分)

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  8. Java哈希表入门

    Java哈希表(Hash Table) 最近做题经常用到哈希表来进行快速查询,遂记录Java是如何实现哈希表的.这里只简单讲一下利用Map和HashMap实现哈希表. 首先,什么是Map和HashMa ...

  9. postfix基础邮件服务

                                                              postfix基础邮件服务  案例1:postfix基础邮件服务 1.1 问题 本例 ...

  10. Windows上安装Docker

    一.下载地址: https://hub.docker.com/editions/community/docker-ce-desktop-windows 二.安装直接下一步下一步就好了 具体可看: ht ...