Convert PowerPoint to PDF/XPS/Image using Java
This article will introduce how to use a 3rd party free Java API to convert a PowerPoint document to other file formats to meet the needs of different occasions.
Installation (2 Methods)
● Download the free API (Free Spire.Presentation for Java) and unzip it, then add the Spire.Presentation.jar file to your project as dependency.
● Directly add the jar dependency to your maven project by adding the following configurations to the pom.xml.
<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>
[Example 1] Convert PowerPoint to PDF:
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class PPTXtoPDF {
public static void main(String[] args) throws Exception {
//create a Presentataion instance
Presentation presentation = new Presentation();
//load the sample PowerPoint file
presentation.loadFromFile("Polar bears.pptx");
//save to PDF file
presentation.saveToFile("toPDF.pdf", FileFormat.PDF);
presentation.dispose();
}
}
[Example 2] Convert PowerPoint to XPS:
import com.spire.presentation.*;
public class PPTtoXPS {
public static void main(String[] args) throws Exception{
//create a presentation instance
Presentation ppt = new Presentation();
//load the sample PowerPoint file
ppt.loadFromFile("Polar bears.pptx");
//save to XPS file
ppt.saveToFile("toXPS.xps", FileFormat.XPS);
ppt.dispose();
}
}
[Example 3] Convert PowerPoint to Image:
3a) Convert PowerPoint to BMP Image:
We could easily convert specific presentation slide to BMP image in Jpg, Png, Tiff, Bmp by using the ISlide.SaveAsImage() method offered by Free Spire.Presentation for Java, here I will convert a .pptx to .png:
import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class ToImage {
public static void main(String[] args) throws Exception {
Presentation ppt = new Presentation();
ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pptx");
//Save PPT document to images
for (int i = 0; i < ppt.getSlides().getCount(); i++) {
BufferedImage image = ppt.getSlides().get(i).saveAsImage();
String fileName = String.format("output/ToImage-%d.png", i);
ImageIO.write(image, "PNG",new File(fileName));
}
ppt.dispose();
}
}
3b) Convert PowerPoint to SVG:
import com.spire.presentation.Presentation;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class ToSVG {
public static void main(String[] args) throws Exception {
Presentation ppt = new Presentation();
ppt.loadFromFile("Polar bears.pptx");
//Save PPT document to images
ArrayList svgBytes = (ArrayList) ppt.saveToSVG();
int count = svgBytes.size();
int len = svgBytes.size();
for (int i = 0; i < len; i++) {
byte[] bytes = (byte[]) svgBytes.get(i);
FileOutputStream stream = new FileOutputStream(String.format("output/ToSVG-%d.svg", i));
stream.write(bytes);
}
ppt.dispose();
}
}