ppt和pptx转图片完整代码,解决2003版和2007版中文乱码问题
2024-10-09 02:17:31
引入所需依赖,注意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(); } } }
生成的图片目录和文件路径在同一位置
最新文章
- [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6
- jquery 杂记
- 《JavaScript语言精粹》小记
- java List 和Map的使用
- TABLE CONTROL隐藏列和固定列的实现
- 0040 Linux 系统管理命令
- SSH整合(1)异常
- USB协议-USB的包结构及包的分类
- SQL 优化原则
- C#委托详解(1):什么是委托
- 【英语】Bingo口语笔记(63) - 一个单词的多种发音
- css3实践—创建3D立方体
- resin config 中文(resin.xml)
- 利用base64函数,对文件进行转码加密
- LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)
- 浅谈OSI七层模型及ICP/IP四层模型
- SUSE12SP3-Mycat(3)Server.xml配置详解
- (三) 查看USB设备
- TCP/IP详解 卷一学习笔记(转载)
- 猜数字游戏,判断输入的数字与系统产生的数字是否一致(Math.random()与if嵌套循环)
热门文章
- [树的深度] Party
- Building Applications with Force.com and VisualForce(Dev401)(十一):Designing Applications for Multiple Users: Proseving Data Quality
- Spring ioc xml 实例化bean 自己实现
- vue配置环境踩坑
- 操作系统-IO与显示器
- java web利用mvc结构实现简单聊天室功能
- NKOJ 7.7练习题A IP地址
- 玩转控件:封装Dev的SearchLookupEdit
- 从测试点点君跨入年薪30W的自动化逍遥君的人生感悟--测试君请进,绝对让你不虚此行!
- Java工程中如何读取配置文件中参数信息