This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Read & Extract Text from PowerPoint Presentation PPTX Files

 
Ranch Hand
Posts: 714
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

TIn this technical tip, we will learn to extract text from different slide shapes inside PowerPoint 2007 presentations by using Aspose.Slides for Java. We will extract text from slide shapes like Placeholders, Auto Shapes, Group Shapes and tables etc.

Below is code example that can traverse through each shape belonging to every slide inside a PPTX presentation and extract text from that on portion level. Since, text is extracted on portion level, so its font related properties will be preserved.


Extracting text from a slide


[Java]


import com.aspose.slides.pptx.*;


public class TextExtract{

  public static void ReadText(TextFrameEx TxtFrame)

  {

    String prText="";

    for(int pgCount=0;pgCount<TxtFrame.getParagraphs().size();pgCount++)

    {

        ParagraphEx Paragraph=TxtFrame.getParagraphs().get(pgCount);

        for(int prCount=0;prCount<Paragraph.getPortions().size();prCount++)

        {

           prText=Paragraph.getPortions().get(prCount).getText();

          System.out.println(prText+"\n");

         }//End Portion Loop

                                                                                   

    }//End Paragraphs Loop


  }

       

  public static void main(String[] args)

  {

    try{

         //Opening presentation

        PresentationEx presentation=new PresentationEx("D:\\ppt\\TestPresentation.pptx");

        //Traversing through all slides

        SlideEx slide;

        ShapesEx shps;

              

        for (int index=0;index<presentation.getSlides().size();index++)

        {

           //Accessing Slides

           slide = presentation.getSlides().get(index);

           //Accessing all shapes in slide

           shps=slide.getShapes();

           ShapeEx shape;     

           //Traversing through all shapes

           for (int shpCount = 0; shpCount < shps.size(); shpCount++)

          {

             shape= shps.get(shpCount);

             if(shape.getPlaceholder() != null)

             {

               //Getting AutoShape from group shapes set

               AutoShapeEx aShape = (AutoShapeEx)shape;

               if (aShape.getTextFrame() != null)

               {

                  //Accessing the text frame of shape

                  TextFrameEx tfText=aShape.getTextFrame();

                  ReadText(tfText);

               }//End Text Frame IF

             }//End AutoShape Check

              else if(shape instanceof AutoShapeEx )

      {

               //Getting AutoShape from group shapes set

               AutoShapeEx aShp = (AutoShapeEx)shape;

               if (aShp.getTextFrame() != null)

               {

                  //Accessing the text frame of shape

                  TextFrameEx tfText=aShp.getTextFrame();

                  ReadText(tfText);

               }//End Text Frame IF

                                                            

             }//End AutoShape Check


              //If shape is a group shape

             else if(shape instanceof GroupShapeEx)

             {

               //Type casting shape to group shape

               GroupShapeEx gShape = (GroupShapeEx)shape;

               //Traversing through all shapes in group shape

               for (int iCount=0;iCount< gShape.getShapes().size();iCount++)

               {

                  if(gShape.getShapes().get(iCount) instanceof AutoShapeEx)

                  {

                    //Getting AutoShape from group shapes set

                    AutoShapeEx aShp = (AutoShapeEx)gShape.getShapes().get(iCount);

                    if (aShp.getTextFrame() != null)

                    {

                       TextFrameEx tfText=aShp.getTextFrame();

                       ReadText(tfText);

                    }//End Text Frame IF

                  }

               }

              }

              //If shape is instance of Table

              else if(shape instanceof TableEx)

              {

                TableEx tTable=(TableEx)shape;

                for(int iCol=0;iCol<tTable.getColumns().size();iCol++)

                {

                  for(int iRow=0;iRow<tTable.getRows().size();iRow++)

                  {

                    TextFrameEx tfText=tTable.get(iCol,iRow).getTextFrame();

                    if(tfText!=null)

                       ReadText(tfText);

                  }//End Row Loop

                }//End Col Loop

               }//End Group Shape IF

                                     

            }//End Shape Loop

                       

         }//End Slide Traversal

        }

        catch(Exception e)

        {

               e.printStackTrace();

        }

       

     }

}


More about Aspose.Slides for Java



Contact Information


Suite 119, 272 Victoria Avenue

Chatswood, NSW, 2067

Australia

Aspose - Your File Format Experts

sales@aspose.com


Phone: 888.277.6734

Fax: 866.810.9465


 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic