Free Support Forum - aspose.com

Problem reading textboxs

Hello,
Thank you for your prompt answer to my previous question!
I would like to read the contents of text boxes in the presentation and remove paragraph/portion if it contains certain text. For some reason I can’t even retrieve the text from the textboxes. I tried using shapes and then getting textframe from shapes and using placeholders to get textholder. Please see attachment for the powerpoint presentation that I used.

Presentation pres=null;
try {
pres = new Presentation(new FileInputStream(new File(“C:/Temp/testAspose.ppt”)) );
} catch ( com.aspose.slides.PptException ppe) {
System.out.println(" Powerpoint exception " + ppe.getLocalizedMessage());
} catch ( IOException e ){
System.out.println(“IO exception occured”);
}
Slides sl = pres.getSlides();

System.out.println("Number of slides: " + sl.size());
for ( int iSlide=0; iSlide < sl.size(); iSlide ++ ){
Shapes shapes = sl.get(iSlide).getShapes();
System.out.println("Number of shapes: " + shapes.size());

for ( int iShape = 0; iShape < shapes.size(); iShape ++ ){
if (shapes.get(iShape) != null) {
Shape shape = shapes.get(iShape);

/// None of the shapes are detected as as textholders???

if ( shapes.get(iShape).isTextHolder() ) {
TextFrame tfrm = shapes.get(iShape).getTextFrame();
System.out.println("Shape textholder: " +shape.isTextHolder() + " Prgs# "+ tfrm.getParagraphs().size() + " Text: " + tfrm.getText());
Paragraphs prgrs = tfrm.getParagraphs();
for ( int pInd =0; pInd < prgrs.size(); pInd++){
System.out.println(“Text in paragraph " + pInd +” is: "+ prgrs.get(pInd).getText());

// look for some text1 and remove paragraph that contains it
if ( prgrs.get(pInd).getText().indexOf(“some text1”) >= 0) prgrs.remove(pInd);


Portions prts = prgrs.get(pInd).getPortions();
for ( int iPort = 0; iPort < prts.size(); iPort++){
Portion prt = prts.get(iPort);
System.out.println(“Text in paragraph " + pInd + " Portion " + iPort + " " + prt.getText());
}
}
}
}
}
}

//
// Can I see the text if I use placeholders??
//
// None of the placeholders are being seen as textholders???

for ( int iSlide=0; iSlide < sl.size(); iSlide ++ ){

Placeholders phs = sl.get(iSlide).getPlaceholders();

System.out.println(“Placeholders# " + phs.size());

for ( int iPhld = 0; iPhld < phs.size(); iPhld++){
if ( phs.get(iPhld) instanceof TextHolder ) {

System.out.println(” Placeholder " + iPhld + " is a TextHodler”);
TextHolder thld = (TextHolder)phs.get(iPhld);


System.out.println( " Slide# " + iSlide + " Ph# " + iPhld + " text: "+thld.getText());
// no method to remove placeholder!

}
}

}


The output is:

Number of slides: 1
Number of shapes: 5
Placeholders# 8




Hi,

You will convert the placeholder into textholder if the following condition meets otherwise you will use Shape.getTextFrame()

Paragraphs paras=null;

if(shape.isTextHolder()==true && shape.getPlaceholder()!=null)

{

//Get the paragraph collections from textholder

Placeholder phld=shape.getPlaceholder();

paras=((TextHolder)phld).getParagraphs();

}

else

{

if(shape.getTextFrame()!=null)

{

//Otherwise, get the paragraph collections from TextFrame

paras=shape.getTextFrame().getParagraphs();

}

}

//Now your job is to work with paragraphs to retrieve/add/remove text