Skip to main content

Convert PowerPoint to PDF, HTML and Image in Java

 

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.

  1. <repositories>  
  2.   
  3.         <repository>  
  4.   
  5.             <id>com.e-iceblue</id>  
  6.   
  7.             <name>e-iceblue</name>  
  8.   
  9.             <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>  
  10.   
  11.         </repository>  
  12.   
  13. </repositories>  
  14.   
  15. <dependencies>  
  16.   
  17.     <dependency>  
  18.   
  19.         <groupId>e-iceblue</groupId>  
  20.   
  21.         <artifactId>spire.presentation.free</artifactId>  
  22.   
  23.         <version>3.9.0</version>  
  24.   
  25.     </dependency>  
  26.   
  27. </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.

  1. import com.spire.presentation.FileFormat;  
  2. import com.spire.presentation.Presentation;  
  3.   
  4. public class ConvertPowerPointToPDFAndHTML {  
  5.     public static void main(String []args) throws Exception {  
  6.         //Create a Presentation instance  
  7.         Presentation ppt = new Presentation();  
  8.         //Load the PowerPoint document  
  9.         ppt.loadFromFile("Sample.pptx");  
  10.           
  11.         //Save the document to PDF  
  12.         ppt.saveToFile("ToPDF.pdf", FileFormat.PDF);  
  13.   
  14.         //Save the document to HTML    
  15.         ppt.saveToFile("ToHTML.html", FileFormat.HTML);    
  16.     }  
  17. }  

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.

  1. import com.spire.presentation.FileFormat;    
  2. import com.spire.presentation.ISlide;   
  3. import com.spire.presentation.Presentation;     
  4.   
  5. public class ConvertSlideToHTML {    
  6.     public static void main(String []args) throws Exception {    
  7.         //Create a Presentation instance    
  8.         Presentation ppt = new Presentation();    
  9.         //Load the PowerPoint document    
  10.         ppt.loadFromFile("Sample.pptx");      
  11.   
  12.         //Get the second slide    
  13.         ISlide slide = ppt.getSlides().get(1);              
  14.   
  15.         //Save the slide to HTML    
  16.         slide.SaveToFile("SlideToHtml.html", FileFormat.HTML);    
  17.     }    
  18. }  

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.
The following code shows how to convert a PowerPoint document to images.

  1. import com.spire.presentation.Presentation;  
  2.   
  3. import javax.imageio.ImageIO;  
  4. import java.awt.image.BufferedImage;  
  5. import java.io.File;  
  6.   
  7. public class ConvertPowerPointToImage {  
  8.     public static void main(String []args) throws Exception {  
  9.         //Create a Presentation instance  
  10.         Presentation ppt = new Presentation();  
  11.         //Load the PowerPoint document  
  12.         ppt.loadFromFile("Sample.pptx");  
  13.   
  14.         //Save the document to images  
  15.         for (int i = 0; i < ppt.getSlides().getCount(); i++) {  
  16.             BufferedImage image = ppt.getSlides().get(i).saveAsImage();  
  17.             String fileName = String.format("Image" + "/" + "ToImage-%1$s.png", i);  
  18.             ImageIO.write(image, "PNG",new File(fileName));  
  19.         }  
  20.     }  
  21. }  

Besides, you can convert the PowerPoint document to specified size of images using the ISlide.saveAsImage(int, int) method.

The result images:









Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <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>               <g

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();         workbook.loadFromFile( "Sample.xlsx" );         //Fit to page         workbook.getConverterSetting().setShee

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <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>