Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Merging Multiple PPTs using java

  • Difficulty Level : Medium
  • Last Updated : 11 Dec, 2020

To merge multiple PowerPoint Presentation files using Java. To achieve this use a Java Library called Apache POI. Apache POI is a project run by the Apache Software Foundation, and previously a sub-project of the Jakarta Project, provides pure Java libraries for reading and writing files in Microsoft Office formats, such as Word, PowerPoint and Excel. Use Apache guide to install the Apache POI libraries for Windows/Linux Systems.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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

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

Input Files: 

file1.pptx

file2.pptx



Output File: 

merged.pptx

Approach: 

  1. Get the current working directory path and list all the presentation files
  2. Create an empty Presentation Object using XMLSlideShow from apache POI package
  3. Iterate through each Presentation File from the list and append slides to the empty Presentation Object
  4. Save the new merged Presentation File

Below is the implementation of the above approach:

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<String> presentationList
            = new ArrayList<String>();
        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!");
    }
}

Output:

Output




My Personal Notes arrow_drop_up
Recommended Articles
Page :