当前位置 博文首页 > m0_46212244的博客:java将ppt转换成图片,图片以幻灯片的备注命

    m0_46212244的博客:java将ppt转换成图片,图片以幻灯片的备注命

    作者:[db:作者] 时间:2021-09-23 13:26

    1.前置:
    (1)需求:ppt文件中的每张幻灯片都输出成图片。图片的名字为每张幻灯片里的备注

    PS:当然你要是想使用ppt里面的文本框文字命名也可以。看自己需求吧。

    2.准备工作
    (1)准备好jar包(下面有些不一定能用到,但是有备无患)

    poi-3.9.jar
    poi-ooxml-3.9.jar
    poi-ooxml-schemas-3.9.jar
    poi-scratchpad-3.9.jar
    xmlbeans-2.3.0.jar
    

    (2)Free Spire.Presentation for Java这个东西也要下载,下载好压缩将jar包导入即可
    下载地址

    3.代码实现

    package readPPTX;
    
    import com.spire.presentation.ISlide;
    import com.spire.presentation.Presentation;
    import org.apache.poi.hslf.HSLFSlideShow;
    import org.apache.poi.hslf.model.Slide;
    import org.apache.poi.hslf.model.TextRun;
    import org.apache.poi.hslf.usermodel.RichTextRun;
    import org.apache.poi.hslf.usermodel.SlideShow;
    
    import java.awt.*;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.Scanner;
    
    /**
     * 需求:将ppt文件中的每张幻灯片都输出成图片。图片的名字为每张幻灯片里的备注
     * 1.使用poi和Free Spire.Presentation for Java
     *      poi实现每张幻灯片的读取
     *      Free Spire.Presentation for Java实现每张幻灯片的备注名字读取
     * 2.注意这里的ppt文件为97版到03版的
     */
    public class Read09 {
        public static void main(String[] args) throws Exception {
            // 要处理的ppt路劲
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要处理的文件:");
            String filePath = scanner.nextLine();
            // 封装输入的路径为File对象
            File dir = new File(filePath);
            if (!dir.exists()) {
                System.out.println("你输入的文件不存在!请检查是否输入正确!!!");
                return;
            }else if (dir.exists() && dir.isDirectory()) {
                System.out.println("你输入的是文件夹而不是文件路径!!!");
                return;
            }
            System.out.println("请输入要保存的路径:");
            String downloadPath = scanner.nextLine();
            File dirPath = new File(downloadPath);
            if (!dirPath.exists() && !dirPath.isDirectory()) {
                dirPath.mkdirs();
            }
            FileInputStream fileInputStream = new FileInputStream(filePath);
            // 获取到这个ppt对象 PS:pptx文件使用XMLSlideShow
            SlideShow slideShow = new SlideShow(new HSLFSlideShow(fileInputStream));
            // 关闭文件读取流
            fileInputStream.close();
            // 这个是Free Spire.Presentation for Java的方法
            Presentation ppt = new Presentation();
            // 加载ppt文档
            ppt.loadFromFile(filePath);
            // 获取大小
            Dimension dimension = slideShow.getPageSize();
            // 获取幻灯片
            Slide[] slides = slideShow.getSlides();
            // 遍历幻灯片
            String notes = "";
            for (int i = 0; i < slides.length; i++) {
                ISlide iSlide = ppt.getSlides().get(i);
                // 获取幻灯片中的备注内容
                if (iSlide.getNotesSlide() == null) {
                    System.out.println("第" + (i + 1) + "张幻灯片备注存在问题!请检查");
                }else {
                    notes = iSlide.getNotesSlide().getNotesTextFrame().getText();
                }
    //            String notes = iSlide.getNotesSlide().getNotesTextFrame().getText();
                // 解决乱码问题
                TextRun[] textRuns = slides[i].getTextRuns();
                for (int k = 0; k < textRuns.length; k++) {
                    RichTextRun[] richTextRuns = textRuns[k].getRichTextRuns();
                    for (int j = 0; j < richTextRuns.length; j++) {
                        richTextRuns[j].setFontIndex(1);
                        richTextRuns[j].setFontName("宋体");
                    }
                }
    
                // 根据幻灯片大小生成图片
                BufferedImage img = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, dimension.width, dimension.height));
                // 核心实现代码
                slides[i].draw(graphics);
    
                // 设定图片保存的位置
                String url = downloadPath + notes + ".png";
    //            System.out.println(url);
                FileOutputStream outputStream = new FileOutputStream(url);
                javax.imageio.ImageIO.write(img, "png", outputStream);
                outputStream.close();
            }
        }
    }
    
    

    PS:希望有人能补充一些知识点

    cs