import org.apache.poi.extractor.POITextExtractor;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.sl.usermodel.PaintStyle;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
public class SlideShowExtractorExample {
public static void main(String[] args) throws Exception {
// SlideShow<XSLFShape, XSLFTextParagraph> slideshow = new XMLSlideShow(new FileInputStream("E:\\Downloads\\simple-ppt-engine-master\\src\\test\\resources\\SmartArt.pptx"));
// SlideShowExtractor<XSLFShape, XSLFTextParagraph> slideShowExtractor = new SlideShowExtractor<XSLFShape, XSLFTextParagraph>(slideshow);
// slideShowExtractor.setCommentsByDefault(true);
// slideShowExtractor.setMasterByDefault(true);
// slideShowExtractor.setNotesByDefault(true);
// String allTextContentInSlideShow = slideShowExtractor.getText();
// System.out.println(allTextContentInSlideShow);
// System.out.println("===========================================================================");
// POITextExtractor textExtractor = slideShowExtractor.getMetadataTextExtractor();
// String metaData = textExtractor.getText();
// System.out.println(metaData);
analysisPpt(FILE_PATH);
}
private static final String FILE_PATH = "E:\\Downloads\\simple-ppt-engine-master\\src\\test\\resources\\SmartArt.pptx";
private static final String OUTPUT_PATH = "E:\\Downloads\\simple-ppt-engine-master\\src\\test\\resources\\SmartArt1.pptx";
private static final String RGBA_TEMPLATE = "rgba(%d,%d,%d,1)";
public static void analysisPpt(String filePath) {
try {
InputStream input = new FileInputStream(filePath);
XMLSlideShow xss = new XMLSlideShow(input);
//getSlides(); 返回幻灯片中找到的所有普通幻灯片
List<XSLFSlide> xslfSlideList = xss.getSlides();
for (XSLFSlide xslfSlide : xslfSlideList) {
//HSLFShape表示工作表中包含的所有形状(幻灯片或注释)
List<XSLFShape> shapes = xslfSlide.getShapes();
handleShapes(shapes);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void handleShapes(final List<XSLFShape> shapes) throws Exception {
for (XSLFShape shape : shapes) {
if (shape instanceof XSLFGroupShape) {
XSLFGroupShape groupShape = ((XSLFGroupShape) shape);
// 对XSLFGroupShape进行递归处理
handleShapes(groupShape.getShapes());
} else if (shape instanceof XSLFTextShape) {
XSLFTextShape xslfTextShape = ((XSLFTextShape) shape);
List<XSLFTextParagraph> textParagraphs = xslfTextShape.getTextParagraphs();
for (XSLFTextParagraph textParagraph : textParagraphs) {
List<XSLFTextRun> runList = textParagraph.getTextRuns();
for (XSLFTextRun run : runList) {
System.out.println(run.getRawText());
}
}
XSLFTextRun textRun = textParagraphs.get(0).getTextRuns().get(0);
Rectangle2D anchor = xslfTextShape.getAnchor();
PaintStyle fontColor = textRun.getFontColor();
Color color = null;
if (Objects.nonNull(fontColor)) {
if (fontColor instanceof PaintStyle.SolidPaint) {
PaintStyle.SolidPaint solidPaint = (PaintStyle.SolidPaint) fontColor;
color = solidPaint.getSolidColor().getColor();
} else if (fontColor instanceof XSLFTexturePaint) {
// 一些大标题是该类型,暂无法获取大标题文字颜色
XSLFTexturePaint texturePaint = (XSLFTexturePaint) fontColor;
System.out.println("todo: XSLFTexturePaint ");
} else {
System.out.println("not match: " + fontColor.getClass());
}
}
String fill = "";
if (Objects.nonNull(color)) {
fill = String.format(RGBA_TEMPLATE, color.getRed(), color.getGreen(), color.getBlue());
}
// System.out.printf("[text]: %s \n[font]: %s [size]: %s [x,y]: (%s,%s) [color]: %s \n", xslfTextShape.getText(),
// textRun.getFontFamily(), textRun.getFontSize(), anchor.getX(), anchor.getY(), fill);
System.out.println("----------------------------");
} else if (shape instanceof XSLFPictureShape) {
XSLFPictureShape xslfPictureShape = ((XSLFPictureShape) shape);
XSLFPictureData pictureData = xslfPictureShape.getPictureData();
// 图片数据
byte[] data = pictureData.getData();
savePicture(data, pictureData.getFileName());
Dimension dimensionInPixels = pictureData.getImageDimensionInPixels();
Rectangle2D anchor = xslfPictureShape.getAnchor();
System.out.printf("[picture name]: %s: [size]: %s * %s [X,Y]: (%s,%s) \n", pictureData.getFileName(), dimensionInPixels.getWidth(),
dimensionInPixels.getHeight(), anchor.getX(), anchor.getY());
} else {
System.out.println("unknown shape:" + shape.getClass());
}
}
}
private static void savePicture(final byte[] data, final String fileName) throws IOException {
FileOutputStream out = new FileOutputStream(OUTPUT_PATH + fileName);
out.write(data);
out.close();
}
}
PPT读取
最新推荐文章于 2024-01-24 14:32:28 发布