Apache POI將PPT轉(zhuǎn)換成圖片實(shí)例代碼
本文主要分享的是關(guān)于Apache POI將PPT轉(zhuǎn)換成圖片的相關(guān)內(nèi)容,簡單介紹了Apache POI,具體內(nèi)容如下。
1、Apache POI 簡介
Apache POI 是用Java編寫的免費(fèi)開源的跨平臺的 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 方式只需要引入兩個就可以,因?yàn)樗麄円蕾嚵似渌麕讉€
<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)換的時候很容易出問題,是因?yàn)镻OI沒有做得很好,圖片有時候容易失真。
// 最核心的代碼 slides[i].draw(graphics);
總結(jié)
以上就是本文關(guān)于Apache POI將PPT轉(zhuǎn)換成圖片實(shí)例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Java實(shí)現(xiàn)定時器的4種方法超全總結(jié)
對于一些特殊的代碼是需要定時執(zhí)行的,下面來看看定時器該如何編寫吧,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)定時器的4種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Spring?Boot?Actuator管理日志的實(shí)現(xiàn)
本文主要介紹了Spring?Boot?Actuator管理日志的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Spring Boot Thymeleaf實(shí)現(xiàn)國際化的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot Thymeleaf實(shí)現(xiàn)國際化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

