Apache POI將PPT轉換成圖片實例代碼
本文主要分享的是關于Apache POI將PPT轉換成圖片的相關內容,簡單介紹了Apache POI,具體內容如下。
1、Apache POI 簡介
Apache POI 是用Java編寫的免費開源的跨平臺的 Java API,Apache POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。
可以查看官方文檔 Apache POI官網(wǎng)
Apache POI操作PPT文檔有兩種方式:
1.POI-HSLF 對應的 Powerpoint ‘97(-2007) 的文件格式 – 后綴名為 .ppt
2.POI-XSLF 對應的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 后綴結尾的文檔。
/**
* ppt2003 文檔的轉換 后綴名為.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);
//及時關閉掉 輸入流
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);
// 重新設置 字體索引 和 字體名稱 是為了防止生成的圖片亂碼問題
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;
}
// 這里設置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑
FileOutputStream out = new FileOutputStream(jpegFile);
ImageIO.write(img, "jpeg", out);
out.close();
}
log.error("PPT轉換成圖片 成功!");
return true;
}
catch (Exception e) {
log.error("PPT轉換成圖片 發(fā)生異常!", e);
}
return false;
}
4、POI-XSLF 方式
POI-XSLF 方式處理PPT文件以 .pptx 后綴結尾的文檔。
/**
* ppt2007文檔的轉換 后綴為.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;
}
// 這里設置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑
FileOutputStream out = new FileOutputStream(jpegFile);
// 寫入到圖片中去
ImageIO.write(img, "jpeg", out);
out.close();
}
log.error("PPT轉換成圖片 成功!");
return true;
}
catch (Exception e) {
log.error("PPT轉換成圖片 發(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)以上錯誤,說明是沒有對應起來使用,應該使用第二種方式來轉換PPT。
有時候核心轉換的時候很容易出問題,是因為POI沒有做得很好,圖片有時候容易失真。
// 最核心的代碼 slides[i].draw(graphics);
總結
以上就是本文關于Apache POI將PPT轉換成圖片實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
Spring?Boot?Actuator管理日志的實現(xiàn)
本文主要介紹了Spring?Boot?Actuator管理日志的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
Spring Boot Thymeleaf實現(xiàn)國際化的方法詳解
這篇文章主要給大家介紹了關于Spring Boot Thymeleaf實現(xiàn)國際化的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-10-10

