Tag Archives: POI scratchpad

Java program to convert a image into PowerPoint PPT

Here is the Java program that converts (uploads) images into PowerPoint .PPT file using Apache POI libraries.

Use (download) the following combination of Apache POI libraries,

poi-3.11-20141221.jar and poi-scratchpad-3.8.jar

Let’s jump into the program


package com.athavan.examples;

import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.model.Fill;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;

public class CreatePresentation {

private SlideShow ppt;
private Slide slide;
private Fill fill;
private int idx;
private FileOutputStream out;

public static void main(String[] args) {
CreatePresentation cp = new CreatePresentation();
try {
cp.ppt = new SlideShow();
cp.ppt.setPageSize(new Dimension(100, 50));
cp.out = new FileOutputStream(
"G:\\Saravanan\\Pulse\\slideshow2.ppt");

// use the for loop if you have many images to be uploaded into PPT

// for (int k = 0; k < 1; k++) {
String tmp = "G:\\Saravanan\\Pulse\\Software.PNG";
System.out.println(tmp);
cp.createSlideWithChart(cp, tmp);
// }
// Save PPT
cp.ppt.write(cp.out);
cp.out.close();
} catch (Exception fnfe) {
fnfe.printStackTrace();
}
}

public void createSlideWithChart(CreatePresentation cp, String chartName) {
try {
slide = cp.ppt.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
fill.setFillType(Fill.FILL_PICTURE);
idx = cp.ppt.addPicture(new File(chartName), Picture.PNG);
fill.setPictureData(idx);
} catch (FileNotFoundException fnfe) {
} catch (IOException ioe) {
}
}

}

Input File: Software.PNG
Output File: slideshow2.ppt

Output:

The above program converts a image into PPT. If we want to upload more images in a folder into PPT, then we can have for loop (which is commented) to achieve the same.

Happy learning and sharing… 🙂