Apache POI將PPT轉(zhuǎn)換成圖片實例代碼
本文主要分享的是關(guān)于Apache POI將PPT轉(zhuǎn)換成圖片的相關(guān)內(nèi)容,簡單介紹了Apache POI,具體內(nèi)容如下。
1、Apache POI 簡介
Apache POI 是用Java編寫的免費開源的跨平臺的 Java API,Apache POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。
可以查看官方文檔 Apache POI官網(wǎng)
Apache POI操作PPT文檔有兩種方式:
1.POI-HSLF 對應(yīng)的 Powerpoint ‘97(-2007) 的文件格式 – 后綴名為 .ppt
2.POI-XSLF 對應(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 方式只需要引入兩個就可以,因為他們依賴了其他幾個
<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);
//及時關(guān)閉掉 輸入流
is.close();
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
log.info("第" + i + "頁。");
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)的錯誤
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)以上錯誤,說明是沒有對應(yīng)起來使用,應(yīng)該使用第二種方式來轉(zhuǎn)換PPT。
有時候核心轉(zhuǎn)換的時候很容易出問題,是因為POI沒有做得很好,圖片有時候容易失真。
// 最核心的代碼 slides[i].draw(graphics);
總結(jié)
以上就是本文關(guān)于Apache POI將PPT轉(zhuǎn)換成圖片實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
SpringMVC架構(gòu)的項目 js,css等靜態(tài)文件導(dǎo)入有問題的解決方法
下面小編就為大家?guī)硪黄猄pringMVC架構(gòu)的項目 js,css等靜態(tài)文件導(dǎo)入有問題的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Java 數(shù)據(jù)流之Broadcast State
這篇文章主要介紹了Java 數(shù)據(jù)流之Broadcast State,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
Java讀寫txt文件時防止中文亂碼問題出現(xiàn)的方法介紹
這篇文章主要介紹了Java讀寫txt文件時防止中文亂碼問題出現(xiàn)的方法,同時需要注意系統(tǒng)默認的文本保存編碼的設(shè)置,需要的朋友可以參考下2015-12-12
使用TraceId在Spring Cloud中實現(xiàn)線上問題快速定位
在微服務(wù)架構(gòu)中,服務(wù)間的互相調(diào)用使得問題定位變得復(fù)雜,在此背景下,TraceId為我們提供了一個在復(fù)雜環(huán)境中追蹤請求路徑和定位問題的工具,本文不僅介紹TraceId的基本概念,還將結(jié)合真實場景,為您展示如何在Spring Cloud中應(yīng)用它2023-09-09
Java多線程事務(wù)回滾@Transactional失效處理方案
這篇文章主要介紹了Java多線程事務(wù)回滾@Transactional失效處理方案,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08

