PPT.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package com.doc.common.utils;
  2. import org.apache.poi.hslf.extractor.PowerPointExtractor;
  3. import org.apache.poi.hslf.usermodel.HSLFSlide;
  4. import org.apache.poi.sl.usermodel.Slide;
  5. import org.apache.poi.sl.usermodel.SlideShow;
  6. import org.apache.poi.sl.usermodel.TextRun;
  7. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.List;
  13. public class PPT {
  14. //直接抽取幻灯片的全部内容
  15. public static String readDoc1(InputStream is) throws IOException {
  16. PowerPointExtractor extractor = new PowerPointExtractor(is);
  17. return extractor.getText();
  18. }
  19. //一张幻灯片一张幻灯片地读取
  20. public static void readDoc2(InputStream is) throws IOException {
  21. SlideShow ss = new XMLSlideShow(is);
  22. List<Slide> slides = ss.getSlides();
  23. for (Slide slide : slides) {
  24. //读取一张幻灯片的标题
  25. String title = slide.getTitle();
  26. System.out.println("标题:" + title);
  27. // //读取一张幻灯片的内容(包括标题)
  28. List<TextRun> runs = slide.getComments();
  29. for (TextRun run:runs) {
  30. System.out.println(run.getRawText());
  31. }
  32. }
  33. }
  34. public static void main(String[] args) {
  35. File file = new File("D:\\SYSTEM\\Desktop\\temp\\测试ppt.pptx");
  36. try (FileInputStream is = new FileInputStream(file)){
  37. SlideShow ppt=new XMLSlideShow(is);
  38. List<HSLFSlide> slides = ppt.getSlides();
  39. for (HSLFSlide slide : slides) {
  40. System.err.println(slide.getTitle());
  41. System.err.println(slide.getComments());
  42. }
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }