When working with PowerPoint Presentations, you may need to merge several presentations into one or split a large presentation into smaller ones. In this article, I will introduce how to merge or split PowerPoint Presentations in Java using Spire.Presentation for Java.
Add Dependencies
Method 1. If you are using maven, you can install the jar of Spire.Presentation for Java into your project by adding the following code to your project’s pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.presentation</artifactId>
<version>7.5.2</version>
</dependency>
</dependencies>
Method 2. If you are not using maven, you can download Spire.Presentation for Java from this website, extract the package and then import the Spire.Presentation.jar under the lib folder into your project as a dependency.
Using The Code
Spire.Presentation for Java provides a Presentation.getSlides.append(ISlide) method which enables you to merge or split PowerPoint presentations by cloning slides from one PowerPoint presentation to another. The following examples will show you how to merge PowerPoint presentations into one and how to split a PowerPoint presentation into multiple presentations in Java.
Example 1. Merge PowerPoint Presentations into One in Java
You can extract all or specified slides of a PowerPoint presentation, and then add them to the end or specified position of a destination presentation. The following are the steps to do so:
- Create an instance of Presentation class.
- Load the first PowerPoint presentation using Presentation.loadFromFile() method.
- Create an instance of Presentation class.
- Load the second PowerPoint presentation using Presentation.loadFromFile() method.
- Loop through the slides in the first presentation, then add each slide to the end of the second presentation using Presentation.getSlides().append() method. If you want to add each slide to a specified position, use the Presentation.getSlides().insert() method instead.
- Save the result presentation to another file using Presentation.saveToFile() method.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class MergePresentations {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation ppt1= new Presentation();
//Load the first presentation
ppt1.loadFromFile("Sample1.pptx");
//Create a Presentation instance
Presentation ppt2 = new Presentation();
//Load the second presentation
ppt2.loadFromFile("Sample2.pptx");
//Loop through the slides of the first presentation
for(int i = 0; i < ppt1.getSlides().getCount(); i++) {
//Append the slides of the first presentation to the end of the second presentation
ppt2.getSlides().append(ppt1.getSlides().get(i));
//Insert the slides of the first presentation into the specified position of second presentation
//ppt2.getSlides().insert(0, ppt1.getSlides().get(i));
}
//Save the result presentation to another file
ppt2.saveToFile("MergePresentations.pptx", FileFormat.PPTX_2013);
}
}
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2022/06/mergepptresult.png?w=820)
Example 2. Split a PowerPoint Presentation into Multiple Presentations in Java
You can split a PowerPoint presentation by each slide or by slide range. The following are the steps to split a PowerPoint presentation by each slide:
- Create an instance of Presentation class.
- Load the first PowerPoint presentation using Presentation.loadFromFile() method.
- Loop through the slides in the presentation.
- Within the loop, create an instance of Presentation class, remove the default slide, then add each slide of the source presentation to the new presentation using Presentation.getSlides().append() method. After that, save the result presentation to file using Presentation.saveToFile() method.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class SplitPresentationByEachPage {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation ppt = new Presentation();
//Load the presentation
ppt.loadFromFile("Sample1.pptx");
//Loop through the slides in the presentation
for (int i = 0; i < ppt.getSlides().getCount(); i++)
{
//Create a new Presentation
Presentation newPPT = new Presentation();
//Remove the default slide
newPPT.getSlides().removeAt(0);
//Extract each slide from the source presentation and append it to the new presentation
newPPT.getSlides().append(ppt.getSlides().get(i));
//Save the result presentation to file
newPPT.saveToFile(String.format("split/split-%d.pptx",i), FileFormat.PPTX_2013);
}
}
}
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2022/06/splitppt.png?w=534)