📜  使用Java合并多个PPT

📅  最后修改于: 2022-05-13 01:55:29.271000             🧑  作者: Mango

使用Java合并多个PPT

使用Java合并多个 PowerPoint 演示文稿文件。为此,请使用名为 Apache POI 的Java库。 Apache POI 是 Apache Software Foundation 运行的一个项目,之前是 Jakarta Project 的子项目,提供纯Java库,用于读写 Microsoft Office 格式的文件,例如 Word、PowerPoint 和 Excel。使用 Apache 指南为 Windows/Linux 系统安装 Apache POI 库。

例子:

Input : file1.pptx, file2.pptx
Output: merged.pptx

Input : file1.pptx file2.pptx file3.pptx
Output: merged.pptx

输入文件:

文件1.pptx

文件2.pptx

输出文件:

合并.pptx

方法:

  1. 获取当前工作目录路径并列出所有演示文件
  2. 使用来自 apache POI 包的XMLSlideShow创建一个空的演示对象
  3. 遍历列表中的每个演示文稿文件并将幻灯片附加到空的演示文稿对象
  4. 保存新合并的演示文稿文件

下面是上述方法的实现:

Java
// Merging Multiple PPTs using java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.util.*;
  
// importing apache POI environment packages
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
  
public class MergePPT {
    public static void main(String args[])
        throws IOException
    {
  
        // creating empty presentation
        XMLSlideShow ppt = new XMLSlideShow();
  
        String path = System.getProperty("user.dir");
        // getting path of current working directory
  
        File file = new File(path);
        // creating empty file using File object
  
        String[] fileList = file.list();
        // returns an array of all files from current
        // working directory
  
        ArrayList presentationList
            = new ArrayList();
        for (String str : fileList) {
            if (str.contains(".pptx"))
                presentationList.add(str);
        }
        // filtering all presentation file paths and
        // appending to presentationList
  
        if (presentationList.isEmpty() == false) {
  
            for (String arg : presentationList) {
  
                FileInputStream inputstream
                    = new FileInputStream(arg);
                // getting current presentation file path in
                // a FileInputStream
                XMLSlideShow src
                    = new XMLSlideShow(inputstream);
                // getting all the slides of the
                // presentation file in a XMLSlideShow
                // object
                for (XSLFSlide srcSlide : src.getSlides()) {
  
                    ppt.createSlide().importContent(
                        srcSlide);
                    // appending each presentation slide to
                    // empty presentation object ppt
                }
            }
            String mergedFile = path + "/merged.pptx";
            // creating new file path
            FileOutputStream out
                = new FileOutputStream(mergedFile);
            // creating the file object
  
            ppt.write(out);
            // saving the changes to the new file
            System.out.println(
                "All files merged successfully!");
            out.close();
        }
        else
            System.out.println(
                "No Presentation files found in current directory!");
    }
}


输出:

输出