本文介绍如何使用Apache POI识别PPT中的图片和文字,获取图片的数据、大小、尺寸、坐标,以及获取文字的字体、大小、颜色、坐标。
官方文档:https://poi.apache.org/components/slideshow/xslf-cookbook.html
官方文档和网上的资料介绍的很少,很多元素,需要一点点尝试才能获取到。
注意:本篇文章针对.pptx结尾的PPT文件
引入依赖:
xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.0.0</version>
</dependency>
核心代码:
java
private static final String FILE_PATH = "/Temp/PPT/test.pptx";
private static final String OUTPUT_PATH = "/Temp/PPT/output/";
private static final String RGBA_TEMPLATE = "rgba(%d,%d,%d,1)";
public static void main(String[] args) {
analysisPpt(FILE_PATH);
}
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();
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();
}