简体   繁体   English

如何使用Apache POI检索PPTX幻灯片名称

[英]How to retrieve pptx slide name with apache poi

Powerpoint slides have internal names that are accessible and modifiable via VBA. Powerpoint幻灯片具有可通过VBA访问和修改的内部名称。 see eg Powerpoint: Manually set Slide Name 参见例如Powerpoint:手动设置幻灯片名称

I would like to access the name via apache poi. 我想通过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? 在Apache POI中获取(甚至设置)pptx文件的幻灯片名称的正确方法是什么?

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. 如果您使用链接的VBA示例,然后尝试上面的代码,则将获得幻灯片名称。 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. 由于幻灯片名称只能通过VBA进行修改-我会改用幻灯片标题,但是当然要取决于您的用例。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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