Learn To Create Microsoft PowerPoint PPTX Presentations In Java

In many enterprise applications, this is essential.

Image for post
Image for post

It is always interesting to be able to generate Microsoft PowerPoint PPTX presentations in an application to propose slideshows to users for example. In that tutorial, you are going to learn how to generate Microsoft PowerPoint presentations in a Java application.

For that, I am going to use the great Apache POI API available just there: https://poi.apache.org/download.html

Note that you can watch this tutorial as a video on YouTube:

Adding Apache POI As A Dependency

First step is to add Apache POI as a Maven dependency in the build file of your Java project:

I have chosen to generate PowerPoint PPTX files in that tutorial but the logic is the same to generate PowerPoint PPT older file format. So, I have added both dependencies in my Maven POM but just the poi-ooxml is needed for my example.

Creating The PowerPoint File

With the Apache POI API, a PowerPoint file is represented by a SlideShow interface. Like I want to generate a PPTX file, I need to create a XMLSlideShow instance. Once the XMLSlideShow instance is created, I need to create a first slide. I get the default XSLFSlideMaster instance and then I get a layout for my slide. Then, I have to call the createSlide method of my XMLSlideShow to create the slide:

I get the title element of my slide which is represented by a XSLFTextShape object. I clear the default text by calling its clearText() method. Then, I add a new text paragraph and a new text. I customize the text by calling some methods like setText(), setFontColor() or still setFontSize().

Next step is to add an image from the resources of my Maven project in the slide. I load the image as an InputStream and I transform it in a byte array. I use the XSLFPictureData object of Apache POI Library to load this byte array. Then, I can create a XSLFPictureShape for which I set an anchor via absolute positioning thanks to a Rectangle object:

I can add a second slide with the same logic. The only difference is that I use a new layout : SlideLayout.TITLE_AND_CONTENT. I set a text for the title and I can create a link on this title by using the createHyperLink method of the XSLFTextRun object created.

For the content of my slide, I add just some texts:

I add a third slide to discover how to define lists. First, I create a new text paragraph by calling the addNewTextParagraph() method of the XSLFTextShape object representing the content of my slide. I pass true to the setBullet method to define the paragraph as a list. Then, I define the indent level via the setIndentLevel method. I add a new text on a new paragraph which will be the second entry of my list. Finally, I end the list with a second level of indentation and a specific bullet numbering. For that, I call the setBulletAutoNumber method on the paragraph concerned:

Generating The PowerPoint File

Finally, I have to generate the PowerPoint file by writing the content of my XMLSlideShow instance to a file. For that, I create a FileOutputStream instance with the name for my file and then I pass this instance as a parameter of the write method of the XMLSlideShow object:

Complete Source Code Of The PowerPoint Class

The complete source code of my PowerPoint class is available just below:

Final Result

By executing the PowerPoint class on a JVM, the MyPresentation.pptx file should be generated on your machine and you should have the following result:

Image for post
Image for post

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store