【问题标题】:How can I retrieve images from a .pptx file using MS Open XML SDK?如何使用 MS Open XML SDK 从 .pptx 文件中检索图像?
【发布时间】:2014-01-11 02:14:25
【问题描述】:

我开始尝试Open XML SDK 2.0 for Microsoft Office

我目前能够执行某些操作,例如检索每张幻灯片中的所有文本,并获取演示文稿的大小。例如,我是这样处理后者的:

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());
}

现在我想检索给定幻灯片中的嵌入图像。有谁知道如何做到这一点,或者可以向我指出有关该主题的一些文档?

【问题讨论】:

  • 我很好奇 - 为什么是“/9525.0”?动车组到点的标准除数是“/12700”。

标签: c# .net powerpoint openxml openxml-sdk


【解决方案1】:

首先,您需要获取要从中获取图像的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));

【讨论】:

  • 如何将 SlidePart 转换为可以在 imageList 中的实际图像?
  • 这段代码似乎假设您知道图像的文件名 - 对吧?如果我只想检索 PPTX 文件中的第一张图像或 PPTX 文件中的所有图像怎么办?
  • 有什么办法可以将所有幻灯片转换成图片或svg?
【解决方案2】:

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

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

希望这会有所帮助。

【讨论】:

  • 问题是“如何使用 MS Open XML SDK 从 .pptx 文件中检索图像?”并且您正在提供手动解决方案?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多