【问题标题】:Apache POI powerpoint, image filename incorrectApache POI powerpoint,图像文件名不正确
【发布时间】:2021-10-22 13:31:12
【问题描述】:

我想用 Apache POI 获取我的 powerpoint 图像的名称,但它不正确,它总是“image1.jpeg”(或不同的数字)

ppt.getPictureData().forEach(xslfPictureData - > {
  String filename = xslfPictureData.getFileName();
});

要自定义图像的名称,我会转到“选择面板”。 custom image name

没有办法获取图片名称或id吗?

【问题讨论】:

  • 您从 xslfPictureData.getFileName() 获得的文件名基于 pptx 文件中出现的文件名 - pptx 基本上是一个 zip 文件 - image1 通常是文件名 - 是你确定你的自定义图像名称真的被应用了吗?

标签: apache-poi powerpoint


【解决方案1】:

XMLSlideShow.getPictureData 从幻灯片中获取所有嵌入的图片。但是为了在幻灯片上显示这些图片,它们被放置在幻灯片上的图片形状中。

您的链接图像显示的是幻灯片上图片形状的名称。这与嵌入图片文件的文件名不同。嵌入图片文件的文件名为image1.*, image2.*, .... image[n].*。幻灯片上的图片形状名称可以自定义。

要获得那些XSLFPictureShapes,需要遍历幻灯片的形状并检查是否有XSLFPictureShapes。

到目前为止,apache poi 还没有提供从XSLFPictureShape 获取名称的方法。因此,如果需要,需要使用底层的CT-Objects 来获取名称。

下面的完整示例显示了这一点。

import java.io.FileInputStream;

import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import org.openxmlformats.schemas.presentationml.x2006.main.*;

public class PPTXGetPictureShapes {
        
 static String getName(XSLFPictureShape pictureShape) {
  String name = null;
  org.apache.xmlbeans.XmlObject xmlObject = pictureShape.getXmlObject();
  if (xmlObject instanceof CTPicture) {
   CTPicture ctPicture = (CTPicture)xmlObject;
   CTPictureNonVisual nvSpPr = ctPicture.getNvPicPr();
   if (nvSpPr != null) {
    CTNonVisualDrawingProps cnv = nvSpPr.getCNvPr();
    if (cnv != null) {
     name = cnv.getName();
    }
   }
  }      
  return name;
 }
      
 public static void main(String[] args) throws Exception {
     
  XSLFSlideShowFactory factory = new XSLFSlideShowFactory();
  XMLSlideShow slideShow = factory.create(new FileInputStream("./PPTXPresentation.pptx"));
  
  for (XSLFSlide slide : slideShow.getSlides()) {
   System.out.println(slide);        
   for (XSLFShape shape : slide.getShapes()) {
    System.out.println(shape); 
    
    if (shape instanceof XSLFPictureShape) {
     XSLFPictureShape pictureShape = (XSLFPictureShape)shape;
     String pictureShapeName = getName(pictureShape);
     System.out.println(pictureShapeName); 
    }
    
   }       
  }
 }
}

顺便说一句:如果你有XSLFPictureShape,你也可以从中得到XSLFPictureData来获取嵌入图片的字节数据。所以不需要XMLSlideShow.getPictureData

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 2013-06-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多