1

Can someone help me how to read pptx file in java?i would prefer if this can be read with apache POI, i have been searched this tutorial but i can't find it.I've been successfully read the ppt file with this code :

try {
    FileInputStream fis = new FileInputStream(file);
    fs = new POIFSFileSystem(fis);
    HSLFSlideShow show = new HSLFSlideShow(fs);
    SlideShow ss = new SlideShow(show);
    Slide[] slides=ss.getSlides();
    for (int x = 0; x < slides.length; x++) {
        System.out.println("Slide = " + (x + 1) + " :" + slides[x].getTitle());

        TextRun[] runs = slides[x].getTextRuns();
        for (int i = 0; i < runs.length; i++) {
            TextRun run = runs[i];
            if (run.getRunType() == TextHeaderAtom.TITLE_TYPE) {
                System.out.println("Slide title " + (i + 1) + ": " + run.getText());
            } else {
                System.out.println("Slide text run " + (i + 1) + ": "  + run.getRunType() + " : " + run.getText());
            }
        }
    }
} catch (IOException ioe) {
    ioe.printStackTrace();
}

Can someone tell me what part of this code must be modified to read pptx file?

Benoît Guédas
  • 801
  • 7
  • 25
user1290932
  • 207
  • 1
  • 6
  • 19

3 Answers3

3

According to http://poi.apache.org/slideshow/index.html you need to use a separate set of classes to read OOXML .pptx files. There's example code in the cookbook: http://poi.apache.org/slideshow/xslf-cookbook.html

Arne
  • 596
  • 5
  • 9
  • in the example, it's just explain how to read but only to append a slide on that file, not how to read the entire slide and show it to user. – user1290932 May 21 '12 at 12:33
  • 2
    When you have a XSLFSlide object you can use .getShapes() to get all shapes in the slide. If the shape is a XSLFTextShape you can use .getTextType() to check if it's a title, .getTextParagraphs() to get the paragraphs and .getTextRuns() on the paragraphs to get the text runs with the text. That should give you similar output to the code in your question. – Arne May 21 '12 at 13:27
  • can you give me some example how to use XSLFTextShape? i have try the .getShape() method and i print it in console but i receive this [Lorg.apache.poi.xslf.usermodel.XSLFShape;@3632112e as the result. – user1290932 May 22 '12 at 03:51
  • 1
    You have to use casting as in the example code. See the lines near instanceof XSLFTextShape. – Arne May 22 '12 at 07:28
3

Following link gives the complete code to read data from ppt... http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/DataExtraction.java

To provide a brief summary I can note the steps: First create the slide object using

XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("url of your ppt"));

Get List of Sides in your ppt using:

final List<XSLFSlide> slide = ppt.getSlides();

Iterate through the slides and get a list of shapes in each slide using:

List<XSLFShape> shapes = selectedslide.getShapes(); 

Check the type of shape, it should be either XSLFTextShape or XSLFPictureShape. Then you can process and display the data accordingly.

Here is a link for further reading about shapes.
https://poi.apache.org/apidocs/org/apache/poi/xslf/usermodel/XSLFShape.html

The response by @Arne de Bruijn Hope this helps.

Saurabh Shetty
  • 395
  • 1
  • 5
  • 14
0

According to apache poi release notes, version 3.8 can be used to read PPTX. You have to check out the documentation though.

kostas
  • 1,959
  • 1
  • 24
  • 43
  • I'm using poi version 3.8 but i think it's very difficult to me to understands the documentation in reading pptx file. – user1290932 May 21 '12 at 12:06