OCJP

Read ppt File and pptx file using java code example

Hello earlier we discussed how to read Microsoft Word File (doc and  docx) using Java Code. Now we will discuss about reading Microsoft Power Point Presentation Text Files (ppt or pptx) using Java Code.

For PPT Extention

public static void readPPT(String path)
{
try {
File file=new File(path);
FileInputStream fis = new FileInputStream(file);
POIFSFileSystem 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();
}
}

For pptx Extension

public static void readPPTXFile(String path)
{
try
{
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(path));
XSLFSlide[] slides = ppt.getSlides();

for(int i=0;i<slides.length;i++)
{
XSLFSlide slide = slides[i];
String title=slide.getTitle();
List<DrawingParagraph> data = slide.getCommonSlideData().getText();
System.out.println(title);

for(int j=0;j<data.size();j++)
{
DrawingParagraph para = data.get(j);
System.out.println(para.getText());
}
}
}catch(Exception E)
{

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?