Java: Reorder Slides in a PowerPoint Document

Presenting information in a logical sequence is vital for an effective PowerPoint presentation. Reordering slides in a PowerPoint document gives you the flexibility to fine-tune your presentation and ensure that it delivers your message with maximum impact. By organizing your slides strategically, you can create a dynamic and engaging presentation experience.

In this article, you will learn how to reorder slides in a PowerPoint document in Java using the Spire.Presentation for Java library.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.10.2</version>
    </dependency>
</dependencies>
    

Reorder Slides in a PowerPoint Document in Java

To rearrange the slides in a PowerPoint presentation, you need to create two presentation objects. One object will be used to load the original document while the other will be used to create a new document. By copying the slides from the original document to the new document in the desired order, you can effectively rearrange the slides.

The following are the steps to rearrange slides in a PowerPoint document using Java.

  • Create a Presentationobject.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Specify the slide order within an array.
  • Create another Presentation object for creating a new presentation.
  • Add the slides from the original document to the new presentation in the specified order using Presentation.getSlides().append() method.
  • Save the new presentation to a PPTX file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class ReorderSlides {

    public static void main(String[] args) throws Exception {

        // Create a Presentation object
        Presentation presentation = new Presentation();

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        // Specify the new slide order within an array
        int[] newSlideOrder = new int[] { 3, 4, 1, 2 };

        // Create another Presentation object
        Presentation new_presentation = new Presentation();

        // Remove the default slide
        new_presentation.getSlides().removeAt(0);

        // Iterate through the array
        for (int i = 0; i < newSlideOrder.length; i++)
        {
            // Add the slides from the original PowerPoint file to the new PowerPoint document in the new order
            new_presentation.getSlides().append(presentation.getSlides().get(newSlideOrder[i] - 1));
        }

        // Save the new presentation to file
        new_presentation.saveToFile("output/NewOrder.pptx", FileFormat.PPTX_2019);

        // Dispose resources
        presentation.dispose();
        new_presentation.dispose();
    }
}

Java: Reorder Slides in a PowerPoint Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.