AndyW
AndyW

Reputation: 440

Retrieving a named textBox from ppt with Apache Poi

I have a powerpoint presentation slide with named TextBoxes. I want to be able to change the text from a Java application in a particular named textBox. I have done similar with Excel where I have a method that accepts a String representing the name, then steps through each textBox in the excel sheet and compares the name of the textBox to the supplied name and returns the TextBox object. I used similar code for Powerpoint and it does not work. On writing a test routine below I found that.getShapeName() basically returns the shapeType and not the actual name that I have given it in ppt.

Can anyone help me find a way to get the names of the ppt textBoxes?

private void getTextBox() {
    for (HSLFShape myShape : slide.getShapes()) {
        if (myShape instanceof HSLFTextBox) {
            myTextBox = (HSLFTextBox) myShape;
            System.out.println(myTextBox.getShapeName() + " " + myTextBox.getShapeType());
        }
    }
}

This routine outputs the following:

TextBox TEXT_BOX

TextBox TEXT_BOX

TextBox TEXT_BOX

TextBox TEXT_BOX

TextBox TEXT_BOX

Upvotes: 1

Views: 1266

Answers (1)

kiwiwings
kiwiwings

Reputation: 3446

This seems to be an error in HSLF, as the current implementation doesn't make sense, i.e. returning the name of the shape type when getShapeName() is called :(
I'll fix this in the next release, i.e. POI 4.0.1.

As a workaround you can use this:

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.ddf.EscherComplexProperty;
import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.util.StringUtil;

public class ShapeName {
    public static void main(String[] args) throws IOException {
        FileInputStream is = new FileInputStream("shapeName.ppt");
        HSLFSlideShow ppt = new HSLFSlideShow(is);
        is.close();

        HSLFSlide slide = ppt.getSlides().get(0);

        for (HSLFShape shape : slide.getShapes()) {
            EscherComplexProperty ep = HSLFShape.getEscherProperty(shape.getEscherOptRecord(), EscherProperties.GROUPSHAPE__SHAPENAME);
            String name;
            if (ep != null) {
                name = StringUtil.getFromUnicodeLE(ep.getComplexData());
            } else {
                name = shape.getShapeName()+shape.getShapeId();
            }
            System.out.println(name);
        }

        ppt.close();
    }
}

Upvotes: 3

Related Questions