Java 读取PowerPoint文档中的文本内容和图片 |
您所在的位置:网站首页 › 提取ppt中的文字到word文档 › Java 读取PowerPoint文档中的文本内容和图片 |
这篇文章介绍如何在Java应用程序中读取PowerPoint文档中的文本内容和图片。 使用组件:Free Spire.Presentation for Java 导入jar文件 在开始前,我们需要导入jar文件。下载Free Spire.Presentation for Java并解压缩,然后从lib文件夹下,导入jar包到你的Java应用程序中。 代码示例 读取文本内容 import com.spire.presentation.*; import java.io.FileWriter; public class ExtractText { public static void main(String[] args) throws Exception { //创建Presentation实例 Presentation ppt = new Presentation(); //加载PowerPoint 文档 ppt.loadFromFile("Input.pptx"); StringBuilder buffer = new StringBuilder(); //遍历幻灯片并提取文本 for (Object slide : ppt.getSlides()) { for (Object shape : ((ISlide) slide).getShapes()) { if (shape instanceof IAutoShape) { for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) { buffer.append(((ParagraphEx) tp).getText()); } } } } //写入到.txt文件 FileWriter writer = new FileWriter("ExtractText.txt"); writer.write(buffer.toString()); writer.flush(); writer.close(); } }读取图片 1) 读取所有图片 import com.spire.presentation.Presentation; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class ExtractImages { public static void main(String[] args) throws Exception { //创建Presentation实例 Presentation ppt = new Presentation(); //加载PowerPoint文档 ppt.loadFromFile("Images.pptx"); //提取文档中的所有图片 for (int i = 0; i < ppt.getImages().getCount(); i++) { //Extract images from the PowerPoint document BufferedImage image = ppt.getImages().get(i).getImage(); ImageIO.write(image, "PNG", new File(String.format("presentation/" + "extractImage-%1$s.png", i))); } } }2) 读取指定幻灯片上的图片 import com.spire.presentation.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class ExtractImages { public static void main(String[] args) throws Exception { //创建Presentation实例 Presentation ppt = new Presentation(); //Load the PowerPoint document ppt.loadFromFile("Images.pptx"); //获取第一张幻灯片 ISlide slide = ppt.getSlides().get(0); //遍历幻灯片上的所有形状并提取图片 for(int i = 0; i< slide.getShapes().getCount(); i++) { IShape shape = slide.getShapes().get(i); if(shape instanceof SlidePicture) { SlidePicture pic = (SlidePicture) shape; BufferedImage image = pic.getPictureFill().getPicture().getEmbedImage().getImage(); ImageIO.write(image, "PNG", new File(String.format("slide/" + "extractImage-%1$s.png", i))); } if(shape instanceof PictureShape) { PictureShape ps = (PictureShape) shape; BufferedImage image = ps.getEmbedImage().getImage(); ImageIO.write(image, "PNG", new File(String.format("slide/" + "extractImage-%1$s.png", i))); } } } }
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |