| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package com.doc.common.utils;
- import org.apache.poi.hslf.extractor.PowerPointExtractor;
- import org.apache.poi.hslf.usermodel.HSLFSlide;
- import org.apache.poi.sl.usermodel.Slide;
- import org.apache.poi.sl.usermodel.SlideShow;
- import org.apache.poi.sl.usermodel.TextRun;
- import org.apache.poi.xslf.usermodel.XMLSlideShow;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
- public class PPT {
- //直接抽取幻灯片的全部内容
- public static String readDoc1(InputStream is) throws IOException {
- PowerPointExtractor extractor = new PowerPointExtractor(is);
- return extractor.getText();
- }
- //一张幻灯片一张幻灯片地读取
- public static void readDoc2(InputStream is) throws IOException {
- SlideShow ss = new XMLSlideShow(is);
- List<Slide> slides = ss.getSlides();
- for (Slide slide : slides) {
- //读取一张幻灯片的标题
- String title = slide.getTitle();
- System.out.println("标题:" + title);
- // //读取一张幻灯片的内容(包括标题)
- List<TextRun> runs = slide.getComments();
- for (TextRun run:runs) {
- System.out.println(run.getRawText());
- }
- }
- }
- public static void main(String[] args) {
- File file = new File("D:\\SYSTEM\\Desktop\\temp\\测试ppt.pptx");
- try (FileInputStream is = new FileInputStream(file)){
- SlideShow ppt=new XMLSlideShow(is);
- List<HSLFSlide> slides = ppt.getSlides();
- for (HSLFSlide slide : slides) {
- System.err.println(slide.getTitle());
- System.err.println(slide.getComments());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
|