Convert PPT(X) to PDF and Images in Java
Tags: java, powerpoint, spirepresentation, slide
Published: 2019-03-14 03:11:37

---
title: Convert PPT(X) to PDF and Images in Java
published: true
description: This blog is going to show you how to convert a PowerPoint file to a PDF documents or multiple PNG files, by using Spire.Presentation for Java.
tags: java,powerpoint,spire.presentation,slide
---

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](https://www.e-iceblue.com/Introduce/presentation-for-java.html). 

Below is a screenshot of the input PowerPoint document.

![Format text content](https://thepracticaldev.s3.amazonaws.com/i/w8e1wjix66kjpbj4hu2n.jpg "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. 

```java
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](https://thepracticaldev.s3.amazonaws.com/i/d5i8p8gwuyppqp97uqlw.jpg "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.
```java
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](https://thepracticaldev.s3.amazonaws.com/i/l0uzyc459ktdd17mvz84.jpg "Format text content")