For various purposes, you may want to convert PowerPoint document to other types of files such as PDF, HTML or Image. In this article, I am going to introduce how to convert a PowerPoint document to PDF, HTML and Image programmatically in Java using Free Spire.Presentation for Java library.
Add Dependencies
Before coding, you need to add the following code to the project’s pom.xml file to include Free Spire.Presentation for Java in your project.
- <repositories>
- <repository>
- <id>com.e-iceblue</id>
- <name>e-iceblue</name>
- <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
- </repository>
- </repositories>
- <dependencies>
- <dependency>
- <groupId>e-iceblue</groupId>
- <artifactId>spire.presentation.free</artifactId>
- <version>3.9.0</version>
- </dependency>
- </dependencies>
The input PowerPoint document

Convert PowerPoint to PDF and HTML
The following are the steps to convert a PowerPoint document to PDF or HTML.
- Create a Presentation instance using Presentation class.
- Call loadFromFile(String) method to load a PowerPoint document.
- Call Presentation.saveToFile(String, FileFormat) method to convert the document to PDF or HTML by providing the output file’s name and file format.
The following code shows how to convert a PowerPoint document to PDF and HTML in Java.
- import com.spire.presentation.FileFormat;
- import com.spire.presentation.Presentation;
- public class ConvertPowerPointToPDFAndHTML {
- public static void main(String []args) throws Exception {
- //Create a Presentation instance
- Presentation ppt = new Presentation();
- //Load the PowerPoint document
- ppt.loadFromFile("Sample.pptx");
- //Save the document to PDF
- ppt.saveToFile("ToPDF.pdf", FileFormat.PDF);
- //Save the document to HTML
- ppt.saveToFile("ToHTML.html", FileFormat.HTML);
- }
- }
Besides, you can also use Free Spire.Presentation for Java to convert a specific PowerPoint slide to HTML. Here is the code for your reference.
- import com.spire.presentation.FileFormat;
- import com.spire.presentation.ISlide;
- import com.spire.presentation.Presentation;
- public class ConvertSlideToHTML {
- public static void main(String []args) throws Exception {
- //Create a Presentation instance
- Presentation ppt = new Presentation();
- //Load the PowerPoint document
- ppt.loadFromFile("Sample.pptx");
- //Get the second slide
- ISlide slide = ppt.getSlides().get(1);
- //Save the slide to HTML
- slide.SaveToFile("SlideToHtml.html", FileFormat.HTML);
- }
- }
The result PDF document:

The result HTML document:

Convert PowerPoint to Image
The following are the steps to convert a PowerPoint document to images.
- Create a Presentation instance using Presentation class.
- Call loadFromFile(String) method to load a PowerPoint document.
- Loop through the slides of the PowerPoint document.
- Call the ISlide.saveAsImage() method to convert every slide of the PowerPoint document to image.
- import com.spire.presentation.Presentation;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.File;
- public class ConvertPowerPointToImage {
- public static void main(String []args) throws Exception {
- //Create a Presentation instance
- Presentation ppt = new Presentation();
- //Load the PowerPoint document
- ppt.loadFromFile("Sample.pptx");
- //Save the document to images
- for (int i = 0; i < ppt.getSlides().getCount(); i++) {
- BufferedImage image = ppt.getSlides().get(i).saveAsImage();
- String fileName = String.format("Image" + "/" + "ToImage-%1$s.png", i);
- ImageIO.write(image, "PNG",new File(fileName));
- }
- }
- }
Besides, you can convert the PowerPoint document to specified size of images using the ISlide.saveAsImage(int, int) method.
The result images:

Comments
Post a Comment