">

Important alert: (current site time 7/16/2013 7:23:30 AM EDT)
 

VB icon

Read & Extract Text from PowerPoint Presentation PPTX Files

Email
Submitted on: 5/16/2012 5:13:57 PM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 2108
 
     

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


 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Read & Extract Text from PowerPoint Presentation PPTX Files
// Description:<p><font face="Arial, Helvetica, sans-serif">TIn this technical tip, we will learn to <a href="http://www.aspose.com/docs/display/slidesjava/Extracting+Text+from+the+Presentation+%28PPTX%29"><strong>extract text from different slide shapes inside PowerPoint 2007 presentations </strong></a>by using Aspose.Slides for Java. We will extract text from slide shapes like Placeholders, Auto Shapes, Group Shapes and tables etc.<br />
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.</font></p>
<p><font face="Arial, Helvetica, sans-serif"><strong>Extracting text from a slide</strong></font></p>
// By: aspose_seo
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7028&lngWId=2//for details.//**************************************

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


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.