Java: Convert Images (PNG, JPG, BMP, etc.) to PowerPoint

Picture files are among the most commonly used types of documents in people's lives. Sometimes, you may want to take all image files in a folder and convert them into slides for a PowerPoint presentation. Depending on your requirements, you can convert images to shapes or slide backgrounds. This article demonstrates how to convert images (in any common image format) to a PowerPoint document in Java using Spire.Presentation for Java.

Install Spire.Presentation for Java

First, 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.6.0</version>
    </dependency>
</dependencies>
    

Convert Images to Backgrounds in PowerPoint in Java

When images are converted as background of each slide in a PowerPoint document, they cannot be moved or scaled. The following are the steps to convert a set of images to a PowerPoint file as background images using Spire.Presentation for Java.

  • Create a Presentation object.
  • Set the slide size type to Sreen16x9.
  • Get the image paths from a folder.
  • Traverse through the images.
  • Get a specific image and append it to the image collection of the document using Presentation.getImages().append() method.
  • Add a slide to the document using Presentation.getSlides().append() method.
  • Set the image as the background of the slide using the methods under SlideBackground object.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

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

public class ConvertImagesAsBackground {

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

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

        //Set slide size type
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

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

        //Get image files from a folder
        File directoryPath = new File("C:\\Users\\Administrator\\Desktop\\Images");
        File[] picFiles  = directoryPath.listFiles();

        //Loop through the images
        for (int i = 0; i < picFiles.length; i++)
        {
            //Add a slide
            ISlide slide = presentation.getSlides().append();

            //Get a specific image
            String imageFile = picFiles[i].getAbsolutePath();

            //Append it to the image collection
            BufferedImage bufferedImage =  ImageIO.read(new FileInputStream(imageFile));
            IImageData imageData = presentation.getImages().append(bufferedImage);

            //Set the image as the background image of the slide
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);
            slide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
            slide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            slide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
        }

        //Save to file
        presentation.saveToFile("output/ImagesToBackground.pptx", FileFormat.PPTX_2013);
    }
}

Java: Convert Images (PNG, JPG, BMP, etc.) to PowerPoint

Convert Images to Shapes in PowerPoint in Java

If you would like the images are moveable and resizable in the PowerPoint file, you can convert them as shapes. Below are the steps to convert images to shapes in a PowerPoint document using Spire.Presentation for Java.

  • Create a Presentation object.
  • Set the slide size type to Sreen16x9.
  • Get the image paths from a folder.
  • Traverse through the images.
  • Get a specific image and append it to the image collection of the document using Presentation.getImages().append() method.
  • Add a slide to the document using Presentation.getSlides().append() method.
  • Add a shape with the size equal to the slide using ISlide.getShapes().appendShape() method.
  • Fill the shape with the image using the methods under FillFormat object.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

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

public class ConvertImageToShape {

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

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

        //Set slide size type
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

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

        //Get image files from a folder
        File directoryPath = new File("C:\\Users\\Administrator\\Desktop\\Images");
        File[] picFiles  = directoryPath.listFiles();

        //Loop through the images
        for (int i = 0; i < picFiles.length; i++)
        {
            //Add a slide
            ISlide slide = presentation.getSlides().append();

            //Get a specific image
            String imageFile = picFiles[i].getAbsolutePath();

            //Append it to the image collection
            BufferedImage bufferedImage =  ImageIO.read(new FileInputStream(imageFile));
            IImageData imageData = presentation.getImages().append(bufferedImage);

            //Add a shape with the size equal to the slide
            IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(0, 0, (float) presentation.getSlideSize().getSize().getWidth(), (float)presentation.getSlideSize().getSize().getHeight()));

            //Fill the shape with image
            shape.getLine().setFillType(FillFormatType.NONE);
            shape.getFill().setFillType(FillFormatType.PICTURE);
            shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            shape.getFill().getPictureFill().getPicture().setEmbedImage(imageData);
        }

        //Save to file
        presentation.saveToFile("output/ImagesToShape.pptx", FileFormat.PPTX_2013);
    }
}

Java: Convert Images (PNG, JPG, BMP, etc.) to PowerPoint

Convert Images to PowerPoint with Customized Slide Size in Java

If the aspect ratio of your images is not 16:9, or they are not in a standard slide size, you can create slides based on the actual size of the pictures. This will prevent the image from being over stretched or compressed. The following are the steps to convert images to a PowerPoint document with customized slide size using Spire.Presentation for Java.

  • Create a Presentation object.
  • Create a PdfUnitConvertor object, which is used to convert pixel to point.
  • Get the image paths from a folder.
  • Traverse through the images.
  • Get a specific image and append it to the image collection of the document using Presentation.getImages().append() method.
  • Get the image width and height, and convert them to point.
  • Set the slide size of the presentation based on the image size using Presentation.getSlideSize().setSize() method.
  • Add a slide to the document using Presentation.getSlides().append() method.
  • Set the image as the background image of the slide using the methods under SlideBackground object.
  • Save the document to a PowerPoint file using Presentation.saveToFile() method.
  • Java
import com.spire.pdf.graphics.PdfGraphicsUnit;
import com.spire.pdf.graphics.PdfUnitConvertor;
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

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

public class CustomizeSlideSize {

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

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

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

        //Get image files from a folder
        File directoryPath = new File("C:\\Users\\Administrator\\Desktop\\Images");
        File[] picFiles  = directoryPath.listFiles();

        //Create a PdfUnitConvertor object
        PdfUnitConvertor convertor = new PdfUnitConvertor();

        //Loop through the images
        for (int i = 0; i < picFiles.length; i++)
        {
            //Get a specific image
            String imageFile = picFiles[i].getAbsolutePath();

            //Append it to the image collection
            BufferedImage bufferedImage =  ImageIO.read(new FileInputStream(imageFile));
            IImageData imageData = presentation.getImages().append(bufferedImage);

            //Get image height and width in pixel
            int height = imageData.getHeight();
            int width = imageData.getWidth();

            //Convert pixel to point
            float widthPoint = convertor.convertUnits(width, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
            float heightPoint= convertor.convertUnits(height, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);

            //Set slide size
            presentation.getSlideSize().setSize(new Dimension((int)widthPoint, (int)heightPoint));

            //Add a slide
            ISlide slide = presentation.getSlides().append();

            //Set the image as the background image of the slide
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);
            slide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
            slide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            slide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
        }

        //Save to file
        presentation.saveToFile("output/CustomizeSlideSize.pptx", FileFormat.PPTX_2013);
    }
}

Java: Convert Images (PNG, JPG, BMP, etc.) to 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.