1. Products
  2.   Presentation
  3.   Java
  4.   Apache POI HSLF



Java Library for Microsoft® PPT Presentation File Formats

Add Slides & Images to Presentations, Convert PPT Files with open-source Free Java API.

What is Apache POI HSLF?

Apache POI HSLF is pure Java implementation for reading, creating, modifying, or rendering PowerPoint presentations. It provides a way to read, create or modify PowerPoint presentations PPT file format. It provides support for extracting data such as text, images, sounds, embedded objects & much more from PowerPoint presentations.

It also supports drawing a shape on a slide, adding hyperlinks, Tables, images, customizing Headers & Footers, creating bulleted lists, retrieving embedded sounds, and much more.

Previous Next

Getting Started with Apache POI HSLF

First of all, you need to have the Java Development Kit (JDK) installed on your system. If you already have it then proceed to the Apache POI's download page to get the latest stable release in an archive. Extract the contents of the ZIP file in any directory from where the required libraries can be linked to your Java program. That is all!

Referencing Apache POI in your Maven-based Java project is even simpler. All you need is to add the following dependency in your pom.xml and let your IDE fetch and reference the Apache POI Jar files.

Apache POI Maven Dependency

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->

Dynamically Create New PPT or Modify Existing Presentations

Apache POI HSLF enables programmers to create new PowerPoint presentations in PPT file formats. Developers can also transform an existing presentation according to their needs. The API also supports features for extracting data such as text, images, sounds, embedded objects & so on from PowerPoint presentations.

Create a PPT file - Java

// create a new PPT file
FileOutputStream fileOutputStream = new FileOutputStream(new File("Slide.ppt"));
// create a new slide show
HSLFSlideShow xmlSlideShow = new HSLFSlideShow();
// save file

Add Slides, Images & Customize Header & Footer to PPT

Developers can easily add new slides and modify existing ones according to their own needs inside a PPT presentation using Apache HSLF API. Developers can now add customized headers and footers into their presentations. It also provides complete support for adding a title for a slide, creating a slide with a predefined layout, working with slide/shape background, and much more.

Insert Images in PPT via Java

  1. Create a new PPT file by using FileOutputStream and pass instance of new File() with output filename as string
  2. Add new slide show using HSLFSlideShow() method
  3. Add new slide using createSlide() method
  4. Get image bytes using IOUtils.toByteArray() method and pass your image in it via FileInputStream() method
  5. Add image to your slide using hslfSlideShow.addPicture(picture, HSLFPictureData.PictureType.PNG) method. The method accepts picture bytes and picture type as arguments
  6. Write and save file

Add Image in PPT - Java

// create a new PPT file
FileOutputStream fileOutputStream = new FileOutputStream(new File("AddImage.ppt"));
// create a new slide show
HSLFSlideShow hslfSlideShow = new HSLFSlideShow();
// create slide
HSLFSlide slide = hslfSlideShow.createSlide();
// load image
byte[] picture = IOUtils.toByteArray(new FileInputStream(new File("apache-poi-logo-min.png")));
// add image
HSLFPictureData hslfPictureData = hslfSlideShow.addPicture(picture, HSLFPictureData.PictureType.PNG);
HSLFPictureShape pictureShape = slide.createPicture(hslfPictureData);
// save file
// close stream

Convert Slides to Image Formats using Java API

Apache POI HSLF API enables Software developers to convert each slide of a PowerPoint Presentation into an image file format inside their Java applications. You can capture slides into java.awt.Graphics2D object (or any other) and serialize it into a PNG or JPEG format. The supported images could be in JPEG, PNG, DIB, and so on.

Convert PPT to Image - Java

//open an existing PPT file
HSLFSlideShow hslfSlideShow = new HSLFSlideShow(new FileInputStream(new File("PPTtoImage.ppt")));
// get dimensions
Dimension pgsize = hslfSlideShow.getPageSize();
java.util.List slide = hslfSlideShow.getSlides();

for (int i = 0; i < slide.size(); i++) {
  BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,BufferedImage.TYPE_INT_RGB);
  Graphics2D graphics = img.createGraphics();

  // clear the drawing area
  graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

  // render

  // create image
  FileOutputStream out = new FileOutputStream("PPTtoImage.png");
  javax.imageio.ImageIO.write(img, "png", out);