Apache POI將PPT轉(zhuǎn)換成圖片實(shí)例代碼
本文主要分享的是關(guān)于Apache POI將PPT轉(zhuǎn)換成圖片的相關(guān)內(nèi)容,簡(jiǎn)單介紹了Apache POI,具體內(nèi)容如下。
1、Apache POI 簡(jiǎn)介
Apache POI 是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,Apache POI提供API給Java程式對(duì)Microsoft Office格式檔案讀和寫的功能。
可以查看官方文檔 Apache POI官網(wǎng)
Apache POI操作PPT文檔有兩種方式:
1.POI-HSLF 對(duì)應(yīng)的 Powerpoint ‘97(-2007) 的文件格式 – 后綴名為 .ppt
2.POI-XSLF 對(duì)應(yīng)的PowerPoint 2007 OOXML 的文件格式 – 后綴名為 .pptx
2、JAR包
POI 操作office需要的jar包:
poi-3.12.jar poi-ooxml-3.12.jar poi-ooxml-schemas-3.12.jar poi-scratchpad-3.12.jar xmlbeans-2.6.0.jar
maven方式引入:
maven 方式只需要引入兩個(gè)就可以,因?yàn)樗麄円蕾嚵似渌麕讉€(gè)
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.12</version> </dependency>
3、POI-HSLF 方式
POI-HSLF 方式處理PPT以 .ppt 后綴結(jié)尾的文檔。
/** * ppt2003 文檔的轉(zhuǎn)換 后綴名為.ppt * @param pptFile ppt文件 * @param imgFile 圖片將要保存的目錄(不是文件) * @return */ public static Boolean doPPT2003toImage(File pptFile,File imgFile,List<String> list) { try { FileInputStream is = new FileInputStream(pptFile); SlideShow ppt = new SlideShow(is); //及時(shí)關(guān)閉掉 輸入流 is.close(); Dimension pgsize = ppt.getPageSize(); Slide[] slide = ppt.getSlides(); for (int i = 0; i < slide.length; i++) { log.info("第" + i + "頁(yè)。"); TextRun[] truns = slide[i].getTextRuns(); for (int k = 0; k < truns.length; k++) { RichTextRun[] rtruns = truns[k].getRichTextRuns(); for (int l = 0; l < rtruns.length; l++) { // 原有的字體索引 和 字體名字 int index = rtruns[l].getFontIndex(); String name = rtruns[l].getFontName(); log.info("原有的字體索引 和 字體名字: "+index+" - "+name); // 重新設(shè)置 字體索引 和 字體名稱 是為了防止生成的圖片亂碼問題 rtruns[l].setFontIndex(1); rtruns[l].setFontName("宋體"); } } //根據(jù)幻燈片大小生成圖片 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)); slide[i].draw(graphics); // 圖片的保存位置 String absolutePath = imgFile.getAbsolutePath()+"/"+ (i + 1) + ".jpeg"; File jpegFile = new File(absolutePath); // 圖片路徑存放 list.add((i + 1) + ".jpeg"); // 如果圖片存在,則不再生成 if (jpegFile.exists()) { continue; } // 這里設(shè)置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑 FileOutputStream out = new FileOutputStream(jpegFile); ImageIO.write(img, "jpeg", out); out.close(); } log.error("PPT轉(zhuǎn)換成圖片 成功!"); return true; } catch (Exception e) { log.error("PPT轉(zhuǎn)換成圖片 發(fā)生異常!", e); } return false; }
4、POI-XSLF 方式
POI-XSLF 方式處理PPT文件以 .pptx 后綴結(jié)尾的文檔。
/** * ppt2007文檔的轉(zhuǎn)換 后綴為.pptx * @param pptFile PPT文件 * @param imgFile 圖片將要保存的路徑目錄(不是文件) * @param list 存放文件名的 list * @return */ public static Boolean doPPT2007toImage(File pptFile,File imgFile,List<String> list) { FileInputStream is = null ; try { is = new FileInputStream(pptFile); XMLSlideShow xmlSlideShow = new XMLSlideShow(is); is.close(); // 獲取大小 Dimension pgsize = xmlSlideShow.getPageSize(); // 獲取幻燈片 XSLFSlide[] slides = xmlSlideShow.getSlides(); for (int i = 0 ; i < slides.length ; i++) { // 解決亂碼問題 XSLFShape[] shapes = slides[i].getShapes(); for (XSLFShape shape : shapes) { if (shape instanceof XSLFTextShape) { XSLFTextShape sh = (XSLFTextShape) shape; List<XSLFTextParagraph> textParagraphs = sh.getTextParagraphs(); for (XSLFTextParagraph xslfTextParagraph : textParagraphs) { List<XSLFTextRun> textRuns = xslfTextParagraph.getTextRuns(); for (XSLFTextRun xslfTextRun : textRuns) { xslfTextRun.setFontFamily("宋體"); } } } } //根據(jù)幻燈片大小生成圖片 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); //圖片將要存放的路徑 String absolutePath = imgFile.getAbsolutePath()+"/"+ (i + 1) + ".jpeg"; File jpegFile = new File(absolutePath); // 圖片路徑存放 list.add((i + 1) + ".jpeg"); //如果圖片存在,則不再生成 if (jpegFile.exists()) { continue; } // 這里設(shè)置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑 FileOutputStream out = new FileOutputStream(jpegFile); // 寫入到圖片中去 ImageIO.write(img, "jpeg", out); out.close(); } log.error("PPT轉(zhuǎn)換成圖片 成功!"); return true; } catch (Exception e) { log.error("PPT轉(zhuǎn)換成圖片 發(fā)生異常!", e); } return false; }
5、可能出現(xiàn)的錯(cuò)誤
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
出現(xiàn)以上錯(cuò)誤,說明是沒有對(duì)應(yīng)起來使用,應(yīng)該使用第二種方式來轉(zhuǎn)換PPT。
有時(shí)候核心轉(zhuǎn)換的時(shí)候很容易出問題,是因?yàn)镻OI沒有做得很好,圖片有時(shí)候容易失真。
// 最核心的代碼 slides[i].draw(graphics);
總結(jié)
以上就是本文關(guān)于Apache POI將PPT轉(zhuǎn)換成圖片實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
SpringBoot前后端接口對(duì)接常見錯(cuò)誤小結(jié)
SpringBoot前后端接口對(duì)接工作時(shí),經(jīng)常遇到請(qǐng)求500,400等問題,本文主要介紹了SpringBoot前后端接口對(duì)接常見錯(cuò)誤小結(jié),感興趣的可以了解一下2022-01-01淺談在頁(yè)面中獲取到ModelAndView綁定的值方法
下面小編就為大家分享一篇淺談在頁(yè)面中獲取到ModelAndView綁定的值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03Java實(shí)現(xiàn)定時(shí)器的4種方法超全總結(jié)
對(duì)于一些特殊的代碼是需要定時(shí)執(zhí)行的,下面來看看定時(shí)器該如何編寫吧,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)定時(shí)器的4種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Spring中使用事務(wù)嵌套時(shí)需要警惕的問題分享
最近項(xiàng)目上有一個(gè)使用事務(wù)相對(duì)復(fù)雜的業(yè)務(wù)場(chǎng)景報(bào)錯(cuò)了。在絕大多數(shù)情況下,都是風(fēng)平浪靜,沒有問題。其實(shí)內(nèi)在暗流涌動(dòng),在有些異常情況下就會(huì)報(bào)錯(cuò),這種偶然性的問題很有可能就會(huì)在暴露到生產(chǎn)上造成事故,那究竟是怎么回事呢?本文就來簡(jiǎn)單講講2023-04-04Spring?Boot?Actuator管理日志的實(shí)現(xiàn)
本文主要介紹了Spring?Boot?Actuator管理日志的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Spring Boot Thymeleaf實(shí)現(xiàn)國(guó)際化的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot Thymeleaf實(shí)現(xiàn)國(guó)際化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10