DEV Community

Alexis
Alexis

Posted on

Java Convert PowerPoint to PDF

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:

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


Enter fullscreen mode Exit fullscreen mode

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.


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);
    }
}


Enter fullscreen mode Exit fullscreen mode

Convert ppt to PDF

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.


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);
    }
}


Enter fullscreen mode Exit fullscreen mode

A single slide to pdf

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.


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);
       }
}


Enter fullscreen mode Exit fullscreen mode

PPT to PDF/A

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.


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);
       }
}


Enter fullscreen mode Exit fullscreen mode

PPT to PDF

Conclusion

In this article, you have learned how to convert PowerPoint presentations to PDF format programmatically in Java with the help of Spire.Presentation. If you have any other questions, please feel free to check the Spire.Presentation forums.

Top comments (1)

Collapse
 
fahadderek profile image
FahadDerek

Good article, convert a PowerPoint document to PDF, HTML and Image programmatically in Java using Free Spire. useful information . دعاء يرجع الزوج لزوجته