简体   繁体   中英

How to get the Slide number using java via Apache POI API

How do I get the number of slides in a .ppt file using java. To access the .ppt we require the Apache POI API - especially the Slide[] class.

I'm using the method getSlideNumber() from here to retrieve the page number but I'm still getting an error. I would like to know how to get the slide numbers.

public final class count {

  public static void main(String args[]) throws Exception {

    File file= new File("C:/Users/THIYAGARAJAN/Desktop/ppt52.ppt");
    FileInputStream is = new FileInputStream(file);
    SlideShow ppt = new SlideShow(is);
    is.close();
    Slide[] slide = ppt.getSlides();
    System.out.println(slide.length);
    for (int i = 0; i < slide.length; i++)  {
      String title = slide[i].getTitle();
      System.out.println("Rendering slide "
                         + slide[i].getSlideNumber()
                         + (title == null ? "" : ": " + title));
    }
  }

}

Is this code correct?

Edit: Here's the error I get in my console:

Exception in thread "main" java.lang.NoSuchFieldError: filesystem 
at org.apache.poi.hslf.HSLFSlideShow.getPOIFSFileSystem(HSLFSlideShow.java:79) 
at org.apache.poi.hslf.EncryptedSlideShow.checkIfEncrypted(EncryptedSlideShow.java:‌​51)
at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:141)
at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:115) 
at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:103)
at org.apache.poi.hslf.usermodel.SlideShow.<init>(SlideShow.java:121) 
at count.count.main(count.java:22) 

have you tried

int getSlideCount()

its in the documentation..

Recently I wanted to count the number of slides I too study for my exam. The problem is very similar to your, even though your problem is 3 years old someone might find it useful.

I give my program a path it then gets all the ppt's in that folder and uses the method getNoOfSlides to count all the slides.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;



import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;



public final class count {

    public static int total=0;

      public static void main(String args[]) throws Exception {

        File files = new File("F:/Dropbox/KFUPM/Sem 151/IAS/IAS final");
        searchInTextFiles(files);
        System.out.println("Total Slides in IAS are: ");
        System.out.println(total);
      }


public static void getNoOfSlides(String path) throws IOException
{
    File file = new File(path);
    System.out.println(path);
    FileInputStream is = new FileInputStream(file);
    XMLSlideShow pps = new XMLSlideShow(is);


    is.close();



    List<XSLFSlide> slides = pps.getSlides();

    total+= slides.size();
    System.out.println(slides.size());


   // System.out.println(slides.size());

    }


public static void searchInTextFiles(File dir) throws IOException {
    File[] a = dir.listFiles();
    for (File f : a) {
        if (f.isDirectory()) {
            searchInTextFiles(f);
        } else if (f.getName().endsWith(".pptx")) {
            String path= f.getAbsolutePath();
            getNoOfSlides(path);

        }
    }


}

}

The exception you have posted is almost straight out of the POI FAQ :

My code uses some new feature, compiles fine but fails when live with a "MethodNotFoundException" or "IncompatibleClassChangeError

You almost certainly have an older version of POI on your classpath. Quite a few runtimes and other packages will ship an older version of POI, so this is an easy problem to hit without your realising.

The best way to identify the offending earlier jar file is with a few lines of java. These will load one of the core POI classes, and report where it came from.

ClassLoader classloader =
         org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL res = classloader.getResource(
         "org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
String path = res.getPath();
System.out.println("Core POI came from " + path);

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