Wednesday, 19 August 2020

[Java] How to Convert PowerPoint to PDF/XPS/Image

PowerPoint is the king of presentation software, but when you need to have it for printing or distribution, an automated and time-saving solution is worth to be adopted that you can convert PowerPoint to other formats (such as PDF, XPS and Image) programmatically in Java especially when there is a bunch of PowerPoint documents that needs to be converted. Keeping this in mind, I’ll show you how to realize the conversion function in Java with Spire.Presentation for Java API.

PowerPoint to PDF/XPS/Image conversions in Java

In this article, we’ll know the following three aspects of PowerPoint conversions using Spire.Presentation for Java.

·   Converting PowerPoint to PDF

·   Converting PowerPoint to XPS

·   Converting PowerPoint to Image(mainly .png and .svg)

Install Spire.Presentation.Jar

You can directly either download Spire.Presentation.Jar or add it in your maven-based project using the following repository and dependency information.

<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>2.6.1</version>

    </dependency>

</dependencies>


Here is a screenshot of the sample PowerPoint file:


Converting PowerPoint to PDF

The following are the simple steps to convert PowerPoint to PDF

·    Create a presentation instance and load a sample file

·    Save slides to PDF file

The following is the full codes:

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class ToPDF {
   
public static void main(String[] args) throws Exception {
       
//create a Presentation instance
       
Presentation presentation = new Presentation();

       
//load the sample PowerPoint file
       
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

       
//save to PDF file
       
presentation.saveToFile("output/toPDF.pdf", FileFormat.PDF);
        presentation.dispose();
    }
}

Output


Converting PowerPoint to XPS

The XML Paper Specification (XPS) format is basically an electronic representation of digital documents based on XML. It is a paginated fixed-layout format that retains the look and feeling of your electronic documents. As a form of electronic paper, the XPS format provides a way in which you can easily create, share, print and save digital documents.

import com.spire.presentation.*;
public class ToXPS {
   
public static void main(String[] args) throws Exception {
       
//create a presentation instance
       
Presentation ppt = new Presentation();
       
//load the sample PowerPoint file
       
ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");
       
//save to XPS file
       
ppt.saveToFile("output/toXPS.xps", FileFormat.XPS);
        ppt.dispose();     }
}
Output

Converting PowerPoint to Image

·   Save PowerPoint to .png Image

By using ISlide.SaveAsImage() method, we could easily convert specific presentation slide to BMP image

in Jpg, Png, Tiff, Bmp. Here we will use .pptx to .png as example:

import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.io.File;
public class ToImage1 {
   
public static void main(String[] args) throws Exception {
       
//create a presentation instance
         Presentation ppt = new Presentation();
       
//load the sample PowerPoint file
         ppt.loadFromFile("C:\\Users\\Test1\\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/ToPNG.png", i);
            ImageIO.write(image,
"PNG",new File(fileName));         }
        ppt.dispose();
    }
}

Output


  • Save PowerPoint to SVG (Scalable Vector Graphics)
SVG, short for scalable vector graphics, is a XML-based file format used to depict two-dimensional

vector graphics. As SVG images are defined in XML text lines, they can be easily searched, indexed,

scripted, and supported by most of the up to date web browsers. Therefore, office documents are often

converted to SVG images for high fidelity viewing.


The following are the simple steps to convert PowerPoint to SVG:

·     Initialize an instance of Presentation class and load a sample PowerPoint document to it;

·     Convert PowerPoint document to byte array and store in a ArrayList object;

·     Initialize an instance of the FileOutputStream class with the specified file path and creation mode.

Full code snippets:

import com.spire.presentation.Presentation;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class ToImage2 {
   
public static void main(String[] args) throws Exception {
       
//create a presentation instance
       
Presentation ppt = new Presentation();
       
//load the sample PowerPoint file
       
ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.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.svg", i));
            stream.write(bytes);
        }
        ppt.dispose();
    }
}
Output












No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...