DEV Community

Leon Davis
Leon Davis

Posted on

Convert PPT/PPTX to PDF Using Java

In our daily work, it's often necessary to convert PPT or PPTX files into PDF format, particularly for generating reports, archiving documents, or sharing presentations. PDF files offer several advantages, including better layout retention, compatibility, and stability. Traditionally, conversion methods rely on software like Microsoft PowerPoint or LibreOffice. However, in automated environments, such as servers, Docker containers, or Linux systems, these methods are limited by installation dependencies and compatibility issues. As a result, many developers prefer using dependency-free Java libraries for PPT to PDF conversion.

This article will introduce how to use the Spire.Presentation for Java library to convert PPT/PPTX files to PDF without relying on Office installations, while supporting batch conversions and various customization options.

1. Installing Spire.Presentation for Java

Before you can start using Spire.Presentation for Java, it must first be installed. You can either install it using Maven or manually download the JAR file.

Using Maven:
Add the following dependency to your pom.xml file:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.presentation</artifactId>
    <version>10.11.4</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Manually Downloading the JAR File:
You can also download the latest JAR file from the official Spire.Presentation website and add it to your project's classpath.

2. Basic Java PPT to PDF Conversion Example

Spire.Presentation for Java offers an easy-to-use API that allows you to convert PPT files with just a few lines of code. Below is a basic example showing how to convert PPT or PPTX files to PDF.

import com.spire.presentation.*;

public class PPTToPDF {
    public static void main(String[] args) {
        // Create a presentation object
        Presentation presentation = new Presentation();

        // Load the PPTX file
        presentation.loadFromFile("input.pptx");

        // Convert and save as PDF
        presentation.saveToFile("output.pdf", FileFormat.PDF);

        // Release resources
        presentation.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Batch Conversion of PPT Files to PDF

If you need to convert multiple PPT files, you can place them in a folder and iterate over the files to convert them in bulk. Here’s an example of how to perform batch conversion:

import java.io.File;
import com.spire.presentation.*;

public class BatchConvertPPTToPDF {
    public static void main(String[] args) {
        String inputDir = "ppt_files";
        String outputDir = "pdf_files";

        File folder = new File(inputDir);
        File[] listOfFiles = folder.listFiles();

        // Create the target folder
        new File(outputDir).mkdirs();

        // Iterate through the files in the directory
        for (File file : listOfFiles) {
            if (file.isFile() && (file.getName().endsWith(".ppt") || file.getName().endsWith(".pptx"))) {
                try {
                    // Load and convert the file
                    Presentation presentation = new Presentation();
                    presentation.loadFromFile(file.getAbsolutePath());

                    // Set the output file path
                    String pdfPath = outputDir + "/" + file.getName().replaceAll("\\.(ppt|pptx)", ".pdf");
                    presentation.saveToFile(pdfPath, FileFormat.PDF);
                    presentation.close();
                    System.out.println("Successfully converted: " + file.getName() + " → " + pdfPath);
                } catch (Exception e) {
                    System.err.println("Conversion failed: " + file.getName() + ", Error: " + e.getMessage());
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Conversion Settings and Optimization

In different scenarios, the generated PDF may require specific settings, such as file size optimization, content layout adjustments, or PDF/A compliance. Spire.Presentation for Java provides a range of customizable options to meet these needs.

Adjusting Slide Size

You can adjust the slide size to meet specific formatting or printing requirements. For example, here's how to set the slide size to A4:

import com.spire.presentation.*;

public class AdjustSlideSizeForPDF {
    public static void main(String[] args) {
        // Create a presentation object
        Presentation presentation = new Presentation();

        // Load the PPTX file
        presentation.loadFromFile("input.pptx");

        // Set slide size to standard A4
        presentation.getSlideSize().setType(SlideSizeType.A4);

        // Auto adjust content to fit the new size
        presentation.setSlideSizeAutoFit(true);

        // Save as PDF
        presentation.saveToFile("resized_output.pdf", FileFormat.PDF);

        // Release resources
        presentation.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Including Hidden Slides

By default, hidden slides are excluded from the PDF conversion. If you need to include hidden slides in the output, you can enable the appropriate option:

import com.spire.presentation.*;

public class IncludeHiddenSlidesInPDF {
    public static void main(String[] args) {
        // Create a presentation object
        Presentation presentation = new Presentation();

        // Load the PPTX file
        presentation.loadFromFile("input.pptx");

        // Get the save options object
        SaveToPdfOption option = presentation.getSaveToPdfOption();

        // Enable the option to include hidden slides
        option.setContainHiddenSlides(true);

        // Save as PDF
        presentation.saveToFile("include_hidden_slides.pdf", FileFormat.PDF);

        // Release resources
        presentation.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Generating PDF/A Compliant Files

For documents that need to be archived, you can generate a PDF that conforms to the PDF/A standard:

import com.spire.presentation.*;

public class GeneratePDFACompliance {
    public static void main(String[] args) {
        // Create a presentation object
        Presentation presentation = new Presentation();

        // Load the PPTX file
        presentation.loadFromFile("input.pptx");

        // Get the save options object
        SaveToPdfOption option = presentation.getSaveToPdfOption();

        // Set PDF compliance to PDF/A-1a
        option.setPdfConformanceLevel(PdfConformanceLevel.Pdf_A1A);

        // Save as PDF
        presentation.saveToFile("pdf_a_output.pdf", FileFormat.PDF);

        // Release resources
        presentation.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Exception Handling

When performing batch conversions, you may encounter various exceptions. Using try-catch blocks helps to handle these exceptions and ensure smooth program execution:

import java.io.File;
import com.spire.presentation.*;

public class SafeConvertPPTToPDF {
    public static void main(String[] args) {
        String inputDir = "ppt_files";
        String outputDir = "pdf_files";

        File folder = new File(inputDir);
        File[] listOfFiles = folder.listFiles();

        // Create the target folder
        new File(outputDir).mkdirs();

        // Iterate through the files in the directory
        for (File file : listOfFiles) {
            if (file.isFile() && (file.getName().endsWith(".ppt") || file.getName().endsWith(".pptx"))) {
                try {
                    // Load and convert the file
                    Presentation presentation = new Presentation();
                    presentation.loadFromFile(file.getAbsolutePath());

                    // Set the output file path
                    String pdfPath = outputDir + "/" + file.getName().replaceAll("\\.(ppt|pptx)", ".pdf");
                    presentation.saveToFile(pdfPath, FileFormat.PDF);
                    presentation.close();
                    System.out.println("Successfully converted: " + file.getName() + " → " + pdfPath);
                } catch (Exception e) {
                    System.err.println("Conversion failed: " + file.getName() + ", Error: " + e.getMessage());
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Conclusion

This article demonstrated how to convert PPT and PPTX files to PDF using Java, covering basic conversions, batch processing, and advanced customization options such as page size adjustments, including hidden slides, and generating PDF/A compliant files. Spire.Presentation for Java is a versatile tool for handling PPT to PDF conversions in various scenarios.

Top comments (0)