Tag Archives: PPT

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… 🙂

Advertisements

Exception in thread “main” java.lang.NoSuchFieldError: filesystem at org.apache.poi.hslf.HSLFSlideShow.getPOIFSFileSystem(HSLFSlideShow.java:79)

I was trying to convert a image into PPT using JAVA with the help of Apache POI libraries.

I got the below exception

Exception in thread "main" java.lang.NoSuchFieldError: filesystem
at org.apache.poi.hslf.HSLFSlideShow.getPOIFSFileSystem(HSLFSlideShow.java:79)
at org.apache.poi.hslf.EncryptedSlideShow.checkIfEncrypted(EncryptedSlideShow.java:57)
at org.apache.poi.hslf.HSLFSlideShow.(HSLFSlideShow.java:141)
at org.apache.poi.hslf.HSLFSlideShow.(HSLFSlideShow.java:115)
at org.apache.poi.hslf.HSLFSlideShow.(HSLFSlideShow.java:103)
at org.apache.poi.hslf.HSLFSlideShow.create(HSLFSlideShow.java:167)
at org.apache.poi.hslf.usermodel.SlideShow.(SlideShow.java:117)
at com.athavan.examples.CreatePresentation.main(CreatePresentation.java:25)

Solution:

I was using poi-3.11-20141221.jar and poi-scratchpad-3.2-FINAL.jar. That’s went wrong here. Then I replaced the poi scratchpad with latest version, the error gone.

poi-scratchpad-3.8.jar worked well.

poi-3.11-20141221.jar and poi-scratchpad-3.8.jar combinations worked!!

Happy learning and sharing 🙂