简体   繁体   中英

How to retrieve pptx slide name with apache poi

Powerpoint slides have internal names that are accessible and modifiable via VBA. see eg Powerpoint: Manually set Slide Name

I would like to access the name via apache poi. I tried:

 public String getName() {
    CTSlide ctSlide = slide.getXmlObject();
    String name=ctSlide.getCSld().getName();
    return name;
  }

but only get empty strings this way if the slide names have only the default name.

What is the proper method to get (or even set) the slide name of a pptx file in Apache POI?

The slide name is by default undefined, therefore you receive an empty string. If you use your linked VBA examples and then try your code above, you get the slide name. The corresponding setter works too ...

As the slide name can be only modified over VBA - I would use the slide title instead, but depends on your use case of course.

public static void main(String[] args) throws Exception {
    // slide name has been set via VBA ...
    FileInputStream fis = new FileInputStream("slidename.pptx");
    XMLSlideShow ppt = new XMLSlideShow(fis);
    fis.close();
    XSLFSlide sl = ppt.getSlides().get(0);
    System.out.println(sl.getXmlObject().getCSld().getName());
    // set slide name via POI and validate it
    sl.getXmlObject().getCSld().setName("new name");
    FileOutputStream fos = new FileOutputStream("slidename2.pptx");
    ppt.write(fos);
    fos.close();
    ppt.close();
    fis = new FileInputStream("slidename2.pptx");
    ppt = new XMLSlideShow(fis);
    fis.close();
    System.out.println(sl.getXmlObject().getCSld().getName());
    ppt.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM