如何使用MS Open XML SDK从.pptx文件检索图像?

本文关键字:文件 pptx 检索 图像 SDK 何使用 MS Open XML | 更新日期: 2023-09-27 18:05:44

我开始尝试Microsoft Office的Open XML SDK 2.0。

我目前能够做某些事情,例如检索每张幻灯片中的所有文本,并获得演示文稿的大小。例如,我这样做:

using (var doc = PresentationDocument.Open(pptx_filename, false)) {
     var presentation = doc.PresentationPart.Presentation;
     Debug.Print("width: " + (presentation.SlideSize.Cx / 9525.0).ToString());
     Debug.Print("height: " + (presentation.SlideSize.Cy / 9525.0).ToString());
}

现在我想检索给定幻灯片中的嵌入图像。有谁知道怎么做或者能给我指出一些关于这个主题的文档吗?

如何使用MS Open XML SDK从.pptx文件检索图像?

首先,您需要获取您想要从中获取图像的SlidePart:

public static SlidePart GetSlidePart(PresentationDocument presentationDocument, int slideIndex)
{
    if (presentationDocument == null)
    {
        throw new ArgumentNullException("presentationDocument", "GetSlidePart Method: parameter presentationDocument is null");
    }
    // Get the number of slides in the presentation
    int slidesCount = CountSlides(presentationDocument);
    if (slideIndex < 0 || slideIndex >= slidesCount)
    {
        throw new ArgumentOutOfRangeException("slideIndex", "GetSlidePart Method: parameter slideIndex is out of range");
    }
    PresentationPart presentationPart = presentationDocument.PresentationPart;
    // Verify that the presentation part and presentation exist.
    if (presentationPart != null && presentationPart.Presentation != null)
    {
        Presentation presentation = presentationPart.Presentation;
        if (presentation.SlideIdList != null)
        {
            // Get the collection of slide IDs from the slide ID list.
            var slideIds = presentation.SlideIdList.ChildElements;
            if (slideIndex < slideIds.Count)
            {
               // Get the relationship ID of the slide.
               string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;
                // Get the specified slide part from the relationship ID.
                SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
                 return slidePart;
             }
         }
     }
     // No slide found
     return null;
}

然后您需要搜索Picture对象,该对象将包含您正在查找的基于图像文件名的图像:

Picture imageToRemove = slidePart.Slide.Descendants<Picture>().SingleOrDefault(picture => picture.NonVisualPictureProperties.OuterXml.Contains(imageFileName));

从Openxml格式获取图像的最简单方法:

使用任何zip存档库从pptx文件的媒体文件夹中提取图像。这将包含文档中的图像。同样,您可以手动将扩展名。pptx替换为。zip,并从媒体文件夹中提取图像。