栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Apache POI将PPT转换成图片实例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Apache POI将PPT转换成图片实例代码

本文主要分享的是关于Apache POI将PPT转换成图片的相关内容,简单介绍了Apache POI,具体内容如下。

1、Apache POI 简介

Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。

可以查看官方文档 Apache POI官网

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 方式只需要引入两个就可以,因为他们依赖了其他几个

    
      org.apache.poi
      poi-ooxml
      3.12
    
    
      org.apache.poi
      poi-scratchpad
      3.12
    

3、POI-HSLF 方式

POI-HSLF 方式处理PPT以 .ppt 后缀结尾的文档。


public static Boolean doPPT2003toImage(File pptFile,File imgFile,List 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("宋体");
				}
			}
			//根据幻灯片大小生成图片
			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转换成图片 发生异常!", e);
	}
	return false;
}

4、POI-XSLF 方式

POI-XSLF 方式处理PPT文件以 .pptx 后缀结尾的文档。


public static Boolean doPPT2007toImage(File pptFile,File imgFile,List 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 textParagraphs = sh.getTextParagraphs();
					for (XSLFTextParagraph xslfTextParagraph : textParagraphs) {
						List textRuns = xslfTextParagraph.getTextRuns();
						for (XSLFTextRun xslfTextRun : textRuns) {
							xslfTextRun.setFontFamily("宋体");
						}
					}
				}
			}
			//根据幻灯片大小生成图片
			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转换成图片 发生异常!", e);
	}
	return false;
}

5、可能出现的错误

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)

出现以上错误,说明是没有对应起来使用,应该使用第二种方式来转换PPT。

有时候核心转换的时候很容易出问题,是因为POI没有做得很好,图片有时候容易失真。

// 最核心的代码
slides[i].draw(graphics);

总结

以上就是本文关于Apache POI将PPT转换成图片实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/142091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号