Adding Text and Shapes with Apache POI

Apache POI is a popular Java library used for working with Microsoft Office documents, including Word, Excel, and PowerPoint files. In this tutorial, we will focus on adding text and shapes to a PowerPoint presentation using Apache POI.

Example Code

Before we dive into the details, let's take a look at a simple example of how to add a text box and a rectangle shape to a PowerPoint slide:

import org.apache.poi.xslf.usermodel.*;

public class TextAndShapesExample {
  public static void main(String[] args) throws Exception {
    XMLSlideShow ppt = new XMLSlideShow();
    XSLFSlide slide = ppt.createSlide();
    XSLFTextBox textBox = slide.createTextBox();
    textBox.setAnchor(new Rectangle2D.Double(50, 50, 200, 100));
    textBox.setText("Hello, world!");
    XSLFAutoShape shape = slide.createAutoShape();
    shape.setAnchor(new Rectangle2D.Double(300, 50, 100, 100));
    ppt.write(new FileOutputStream("output.pptx"));

Step-by-Step Tutorial

  1. Create an instance of the XMLSlideShow class, which represents a PowerPoint presentation.
  2. Create a slide using the createSlide() method.
  3. Create a text box using the createTextBox() method and set its position and size using the setAnchor() method.
  4. Set the text of the text box using the setText() method.
  5. Create a shape using the createAutoShape() method and set its type using the setShapeType() method.
  6. Set the position and size of the shape using the setAnchor() method.
  7. Write the modified presentation to a file using the write() method.

Common Mistakes

  • Forgetting to create a slide before adding text or shapes.
  • Not setting the position and size of the text box or shape correctly.
  • Using invalid shape types or unsupported operations.
  • Missing the necessary dependencies in the project's build configuration.

Frequently Asked Questions (FAQs)

  1. Can I add multiple text boxes and shapes to a slide?

    Yes, you can create multiple text boxes and shapes and position them as needed on a slide.

  2. Can I format the text inside a text box?

    Yes, you can apply formatting options such as font size, color, and alignment to the text inside a text box.

  3. How can I rotate a shape?

    You can use the setRotation() method to rotate a shape by specifying the rotation angle in degrees.

  4. Can I add images or charts using Apache POI?

    Yes, Apache POI provides APIs for adding images and charts to PowerPoint presentations.

  5. Is Apache POI compatible with older versions of Microsoft Office?

    Yes, Apache POI supports various formats and is compatible with different versions of Microsoft Office, including older versions.


In this tutorial, we have learned how to add text and shapes to a PowerPoint presentation using Apache POI. We explored the basic steps, provided example code, highlighted common mistakes, and answered frequently asked questions. With this knowledge, you can now start creating dynamic and customized PowerPoint slides programmatically using Apache POI.