DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Convert PPT(X) to PDF and Images in Java

The advantages of PDF (or image) file format over PowerPoint are:

  • More suitable for archiving, as PDF/image files are fixed in page layout and difficult to be modified.
  • More convenient for delivery, as PDF/image files are compatible with most devices.

For above reasons, you would probably convert your PowerPoint document into a PDF file or multiple image files. This article will show you how to accomplish this task by using Spire.Presentation for Java.

Below is a screenshot of the input PowerPoint document.

Format text content

PPT(X) to PDF

Conversion from PowerPoint to PDF can be very easy using Spire.Presentation. Simply create an object of Presentation class for holding the PowerPoint file that you want to convert, and then invoke the saveToFile() method of the same object to save your document as a PDF file.

import com.spire.presentation.Presentation;
import com.spire.presentation.FileFormat;

public class PPTXtoPDF {

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

        //create a Presentataion instance
        Presentation presentation = new Presentation();

        //load the sample PowerPoint file
        presentation.loadFromFile("C:/Users/Administrator/Desktop/template.pptx");

        //save to PDF file
        presentation.saveToFile("ToPDF.pdf", FileFormat.PDF);
        presentation.dispose();
    }
}

Format text content

PPT(X) to PNG

To export PowerPoint slides to images, you’ll need to save each slide as a BufferdImage using saveAsImage() method in the first place, and then write the image data to an image file in .png file format.

import com.spire.presentation.Presentation;

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

public class ConvertPPTXtoPNG {

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

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

        //load an example PPTX file 
        presentation.loadFromFile("C:/Users/Administrator/Desktop/template.pptx");

        //loop through the slides 
        for (int i = 0; i < presentation.getSlides().getCount(); i++) {

            //save each slide as a BufferedImage 
            BufferedImage image = presentation.getSlides().get(i).saveAsImage();

            //save BufferedImage as PNG file format 
            String fileName =  String.format("ToImage-%1$s.png", i);
            ImageIO.write(image, "PNG",new File(fileName));
        }
        presentation.dispose();

    }
}

Format text content

Top comments (0)