DEV Community

Jane
Jane

Posted on

Operate the Presentation Slides Using Java

PowerPoint is an efficient tool for people to display the information and make a presentation. This article will demonstrate how to operate the presentation slides programmatically using Free Spire.Presentaion for Java. The follwing examples include adding new slide in PowerPoint document, removing an existing slide, hiding slide from a PowerPoint document and changing the Slide Order within a Presentation.

Import jar dependency (2 Methods)
● Download the Free Spire.Presentation for Java and unzip it. Then add the Spire.Presentation.jar file to your project as dependency.

● You can also add the jar dependency to your maven project by adding the following configurations to the pom.xml.

<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.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Add 2 new slides to an existing PowerPoint document:

import com.spire.presentation.*;

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

        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("input1.pptx");

        //add new slide at the end of the document
        presentation.getSlides().append();

        //insert a blank slide before the second slide
        presentation.getSlides().insert(1);

        //Save the document
        presentation.saveToFile("output/AddSlide.pptx", FileFormat.PPTX_2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Add Slides

Hide a slide that we don't want to present to audiences without deleting it:

import com.spire.presentation.*;

public class HideSlides {
    public static void main(String[] args) throws Exception {
        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("input1.pptx");

        //Hide the second slide
        presentation.getSlides().get(1).setHidden(true);

        //Save the document
        presentation.saveToFile("output/HideSlide.pptx", FileFormat.PPTX_2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Hide Slide

Delete a slide from the PowerPoint document:

import com.spire.presentation.*;

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

        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("input1.pptx");

        //Remove the third slide
        presentation.getSlides().removeAt(2);

        //Save the document
        presentation.saveToFile("output/Removeslide.pptx", FileFormat.PPTX_2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Delete Slide

Change the slide order within a PowerPoint document:

import com.spire.presentation.*;

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

        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("input1.pptx");

        //Move the first slide to the second slide position
        ISlide slide = presentation.getSlides().get(0);
        slide.setSlideNumber(2);

        //Save the document
        presentation.saveToFile("output/Reorderslide.pptx", FileFormat.PPTX_2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Reorder Slide

Discussion (0)