Java Convert PowerPoint to PDF
Tags: ppt, pptx, pdf, java
Published: 2022-09-15 06:58:51

We often use PowerPoint to present ideas or complex data in meetings as it helps us to deliver a more memorable message. But when we need to share the file for information purpose, the PDF format is better than PPT/PPTX, as PDF are fixed in page layout and it is difficult to be modified. In this article, we will demonstrate how to convert PowerPoint PPT or PPTX to PDF in Java from the following aspects:

- [Convert a whole PowerPoint to PDF in Java](#convert-powerpoint-to-pdf-in-java)
- [Convert a specific slide to PDF](#convert-a-specific-slide-to-pdf)
- [Convert a PowerPoint document to PDF/A1, PPT to PDF/A2 and PPT to PDF/A3](#convert-a-powerpoint-document-to-pdfa)
- [Set page size when Convert PowerPoint to PDF](#set-page-size-when-convert-powerpoint-to-pdf)

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

```java
<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>7.8.2</version>
    </dependency>
</dependencies>
```

###Convert PowerPoint to PDF in Java

The following steps show you how to convert a whole PowerPoint presentation to PDF

- Initialize an instance of Presentation class.
- Load the PowerPoint presentation using **Presentation.loadFromFile()** method.
- Save it to PDF using **Presentation.saveToFile()** method.

```java
import com.spire.presentation.*;

public class PPTtoPDF {

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

        //Create a Presentation instance
        Presentation ppt = new Presentation();

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

        //Save the whole PowerPoint to PDF
        ppt.saveToFile("ToPdf1.pdf", FileFormat.PDF);
    }
}
```
![Convert ppt to PDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly2s84xc1yuxfxbzi6p0.png)

###Convert a Specific Slide to PDF

If you only want to convert a specific slide to PDF instead of converting the whole presentation, here are the steps. 

- Initialize an instance of Presentation class.
- Load the PowerPoint presentation using **Presentation.loadFromFile()** method.
- Get the desired slide by its index using **Presentation.getSlides().get(slideIndex)** method.
- Save it to PDF using **ISlide.saveToFile(filePath, FileFormat.PDF)** method.

```java
import com.spire.presentation.*;

public class specificSlideToPDF {

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

        //Create a Presentation instance
        Presentation ppt = new Presentation();

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

        //Get the second slide
        ISlide slide= ppt.getSlides().get(1);

        //Save the second slide to PDF
        slide.saveToFile("ToPdf2.pdf", FileFormat.PDF);
    }
}
```

![A single slide to pdf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h807a1ekyyeiku3q3x7g.png)

###Convert a PowerPoint document to PDF/A.

Spire.PDF offers options to set the PDF conformance level as PDF/A1, PDF/A2 and PDF/A3 when saving presentation slides to PDF.

- Initialize an instance of Presentation class.
- Load the PowerPoint presentation using **Presentation.loadFromFile()** method.
- Call **PowerPoint.getSaveToPdfOption().setPdfConformanceLevel()** method to set the PDF conformance level.
- Save it to PDF using **Presentation.saveToFile()** method.

```java
import com.spire.pdf.PdfConformanceLevel;
import com.spire.presentation.*;

public class PPTtoPDF {

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

        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");


        //Set the PDF conformance level as PDF/A
       ppt.getSaveToPdfOption().setPdfConformanceLevel(PdfConformanceLevel.Pdf_A_1_A);


        //Save the slide to PDF
        ppt.saveToFile("ToPdf3.pdf", FileFormat.PDF);
       }
}
```

![PPT to PDF/A](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n00y214c17g2dkymfccs.png)

###Set Page Size when Convert PowerPoint to PDF

The following code shows you how to convert a PowerPoint when its slide size is specified to a PDF:

- Initialize an instance of Presentation class.
- Load the PowerPoint presentation using **Presentation.loadFromFile()** method.
- Call **Presentation.getSlideSize().setType(SlideSizeType.A3)** method to set the slide size as A3.
- Save it to PDF using **Presentation.saveToFile()** method.

```java
import com.spire.presentation.*;

public class PPTtoPDF {

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

        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Set A3 page size
        ppt.getSlideSize().setType(SlideSizeType.A3);

        //Save the slide to PDF
        ppt.saveToFile("ToPdf4.pdf", FileFormat.PDF);
       }
}
```

![PPT to PDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqiqbcy1j1qm7k27cpis.png)
###Conclusion

In this article, you have learned how to convert PowerPoint presentations to PDF format programmatically in Java with the help of [Spire.Presentation](https://www.e-iceblue.com/Introduce/presentation-for-java.html). If you have any other questions, please feel free to check the [Spire.Presentation forums](https://www.e-iceblue.com/forum/spire-presentation-f14.html).