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)));
            }
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值