page 162

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>11.1.1</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.

Sometimes, it's hard to convey information clearly to your audience with words alone, especially if what you are describing is fairly abstract. Adding images to your PowerPoint presentation can help you describe your meaning in a more understandable way and minimize miscommunication between you and your audience. In some cases, you may also want to extract existing images from a PowerPoint presentation. This article will demonstrate how to add or extract images in PowerPoint in Java 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>11.1.1</version>
    </dependency>
</dependencies>

Add an Image to a Slide in Java

Spire.Presentation offers the ISlide.getShapes().appendEmbedImage() method to add an image to a specific slide. The following are the detailed steps:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Get a specific slide by its index using Presentation.getSlides().get(int) method.
  • Add an image to the slide using ISlide.getShapes().appendEmbedImage() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddImageToSlide {
    public static void main(String []args) throws Exception {
        //Initialize an instance of the Presentation class
        Presentation presentation = new Presentation();
        //Load a PowerPoint document
        presentation.loadFromFile("Input.pptx");

        //Get the first slide
        ISlide slide = presentation.getSlides().get(0);

        //Add an image to the slide
        String imageFile = "image.png";
        Rectangle2D.Double rect1 = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 280, 140, 120, 120);
        IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, imageFile, rect1);
        image.getLine().setFillType(FillFormatType.NONE);

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

Java: Add or Extract Images in PowerPoint Documents

Add an Image to Slide Master in Java

A slide master is the top slide that controls all information about the theme, layout, background, color and fonts, which will be inherited by other slides in the presentation. In other words, when you modify the style of the slide master, every slide in the presentation will be changed accordingly, including the ones added later. If you want an image to appear on all your slides, you can add it to one place - the slide master.

Spire.Presentation offers the IMasterSlide.getShapes().appendEmbedImage() method to add an image to a slide master. The following are the detailed steps:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Get a slide master in the document by its index using Presentation.getMasters().get(int) method.
  • Add an image to the slide master using IMasterSlide.getShapes().appendEmbedImage() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddImageToSlideMaster {
    public static void main(String []args) throws Exception {
        //Initialize an instance of the Presentation class
        Presentation presentation = new Presentation();
        //Load a PowerPoint document
        presentation.loadFromFile("Sample.pptx");

        //Get the first slide master in the document
        IMasterSlide master = presentation.getMasters().get(0);

        //Add an image to the slide master
        String image = "logo.png";
        Rectangle2D.Double rect = new Rectangle2D.Double(40, 40, 80, 80);
        IEmbedImage pic = master.getShapes().appendEmbedImage(ShapeType.RECTANGLE, image, rect);
        pic.getLine().getFillFormat().setFillType(FillFormatType.NONE);

        //Add a new slide to the presentation
        presentation.getSlides().append();

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

Java: Add or Extract Images in PowerPoint Documents

Extract Images from a Slide in Java

To extract images from a specific slide, you need to loop through all shapes on the slide, find the shapes that are of SlidePicture or PictureShape type, then call the SlidePicture.getPictureFill().getPicture().getEmbedImage().getImage() or PictureShape.getEmbedImage().getImage() method to retrieve the images. The following are the detailed steps:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Get a specific slide by its index using Presentation.getSlides().get(int) method.
  • Loop through all shapes on the slide.
  • Check if the shape is of SlidePicture or PictureShape type. If the result is true, retrieve the image using SlidePicture.getPictureFill().getPicture().getEmbedImage().getImage() or PictureShape.getEmbedImage().getImage() method.
  • Save the images to PNG files.
  • Java
import com.spire.presentation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractImageFromSlide {
    public static void main(String []args) throws Exception {
        //Initialize an instance of the Presentation class
        Presentation ppt = new Presentation();
        //Load a PowerPoint document
        ppt.loadFromFile("Images.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Loop through all shapes on the slide
        for(int i = 0; i< slide.getShapes().getCount(); i++)
        {
            IShape shape = slide.getShapes().get(i);
            //Extract images from the slide
            if(shape instanceof SlidePicture)
            {
                SlidePicture pic = (SlidePicture) shape;
                BufferedImage image = pic.getPictureFill().getPicture().getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
            if(shape instanceof PictureShape)
            {
                PictureShape ps = (PictureShape) shape;
                BufferedImage image = ps.getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
        }
    }
}

Java: Add or Extract Images in PowerPoint Documents

Extract All Images from a PowerPoint document in Java

To extract all images from a PowerPoint document, you can use the Presentation.getImages() method to get the image collection of the document, then loop through the image collection and call ImageCollection.get(int).getImage() method to retrieve the images. The following are the detailed steps:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Get the image collection of the document using the Presentation.getImages() method.
  • Loop through the image collection and call ImageCollection.get(int).getImage() method to retrieve the images.
  • Save the images to PNG files.
  • Java
import com.spire.presentation.Presentation;
import com.spire.presentation.collections.ImageCollection;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractAllImagesFromPowerPoint {
    public static void main(String []args) throws Exception {
        //Initialize an instance of the Presentation class
        Presentation ppt = new Presentation();
        //Load a PowerPoint document
        ppt.loadFromFile("Images.pptx");

        //Get the image collection of the document
        ImageCollection collection = ppt.getImages();
        
        //Loop through the image collection
        for (int i = 0; i < collection.getCount(); i++) {
            //Retrieve images from the collection
            BufferedImage image = collection.get(i).getImage();
            ImageIO.write(image, "PNG",  new File(String.format("presentation/" + "extractImage-%1$s.png", i)));
        }
    }
}

Java: Add or Extract Images in PowerPoint Documents

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.

Spire.Presentation for Java is a professional PowerPoint API that enables developers to create, read, write, convert and save PowerPoint documents in Java Applications. As an independent Java library, Spire.Presentation doesn't need Microsoft PowerPoint to be installed on system.

A rich set of features can be supported by Spire.Presentation for Java, such as inserting slides, chart, table, shapes, bullets, watermark, hyperlink, digital signatures, audio/video, header/footer, speaker notes, comments, and encrypting/decrypting PPT, paragraph settings, document properties settings, inserting/extracting images, extracting text, setting animation, creating SmartArt etc.

page 162