国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

Apache POI將PPT轉(zhuǎn)換成圖片實例代碼

2024-07-13 10:16:37
字體:
供稿:網(wǎng)友

本文主要分享的是關(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/115642.html">point ‘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)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大邑县| 黑河市| 合水县| 湘乡市| 青铜峡市| 南投市| 镶黄旗| 南丹县| 无棣县| 尼木县| 金秀| 襄樊市| 罗源县| 和静县| 从化市| 桓仁| 沙洋县| 泉州市| 壤塘县| 双辽市| 徐汇区| 高陵县| 江源县| 乐平市| 顺义区| 雅安市| 南和县| 灵璧县| 新沂市| 甘南县| 改则县| 仲巴县| 淮阳县| 玛纳斯县| 屯门区| 呼伦贝尔市| 乌苏市| 隆子县| 开化县| 定结县| 龙岩市|