News Category

Java: Add, Hide or Delete Slides in PowerPoint

2022-12-23 03:27:00 Written by  support iceblue
Rate this item
(0 votes)

Slides are one of the important elements in a PowerPoint document because they carry the information or ideas you would like to present to your audience. When editing a PowerPoint document, it's inevitable that you will perform operations on presentation slides, such as creating new slides, deleting unwanted slides, or hiding slides temporarily. This article will demonstrate how to programmatically add, hide/unhide or delete a PowerPoint slide using Spire.Presentation for Java.

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.4.5</version>
    </dependency>
</dependencies>
    

Add a New Slide at the End of the PowerPoint Document

The Presentation.getSlides().append() method provided by Spire.Presentation for Java allows you to append a new slide after the last slide of a PowerPoint document. The detailed steps are as follows.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Add a new blank slide at the end of the document using Presentation.getSlides().append() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class AddNewSlideinPowerPoint {
    public static void main(String[] args) throws Exception {
        //Initialize an instance of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint document
        presentation.loadFromFile("Sample.pptx");

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

        //Save the result document
        presentation.saveToFile("AddSlide.pptx", FileFormat.PPTX_2013);
    }
}

Java: Add, Hide or Delete Slides in PowerPoint

Insert a New Slide Before a Specific Slide in PowerPoint

Sometimes you may also need to insert a slide before a specific slide to add additional supporting information, and below are the detailed steps to accomplish the task.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Insert a blank slide before a specified slide using Presentation.getSlides().insert() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class InsertSlideinPowerPoint {
    public static void main(String[] args) throws Exception {
        //Initialize an instance of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint document
        presentation.loadFromFile("Sample.pptx");

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

        //Save the result document
        presentation.saveToFile("InsertSlide.pptx", FileFormat.PPTX_2013);
    }
}

Java: Add, Hide or Delete Slides in PowerPoint

Hide or Unhide a Specific Slide in PowerPoint

For the slides that you temporarily do not want to show during a presentation, you can simply hide the slides without deleting them. Below are the steps to hide or unhide a specific PowerPoint slide.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Get a specified slide using Presentation.getSlides().get() method.
  • Hide or unhide the slide by setting the value of ISlide.setHidden() method to true or false.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class HideUnhideSlides {
    public static void main(String[] args) throws Exception {
        //Initialize an instance of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint document
        presentation.loadFromFile("Sample.pptx");

        //Get the third slide
        ISlide slide = presentation.getSlides().get(2);

        //Hide the slide
        slide.setHidden(true);

        //Unhide the slide
        //slide.setHidden(false);

        //Save the result document
        presentation.saveToFile("HideSlide.pptx", FileFormat.PPTX_2010);
    }
}

Java: Add, Hide or Delete Slides in PowerPoint

Delete a Specific Slide from a PowerPoint Document

If you want to remove a unnecessary slide from the document, you can use the Presentation.getSlides().removeAt(int index) method. The detailed steps are as follows.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Remove a specified slide from the document using Presentation.getSlides().removeAt() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class DeletePowerPointSlide {
    public static void main(String[] args) throws Exception {
        //Initialize an instance of Presentation class
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint document
        presentation.loadFromFile("Sample");

        //Remove the first slide
        presentation.getSlides().removeAt(0);

        //Save the document
        presentation.saveToFile("RemoveSlide.pptx", FileFormat.PPTX_2013);
    }
}

Java: Add, Hide or Delete Slides in PowerPoint

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.

Additional Info

  • tutorial_title:
Last modified on Friday, 23 December 2022 01:11