c# - 从 powerpoint 文件中检索图像名称

标签 c# .net openxml-sdk presentation

我需要从 pptx 文件中的图像中检索图像文件名。我已经从图像中获得了流,但我没有得到图像文件的名称...这是我的代码:

private static Stream GetParagraphImage(DocumentFormat.OpenXml.Presentation.Picture     picture, DocumentFormat.OpenXml.Packaging.PresentationDocument presentation, ref     MyProject.Import.Office.PowerPoint.Presentation.Paragraph paragraph)
{
        // Getting the image id
        var imageId = picture.BlipFill.Blip.Embed.Value;
        // Getting the stream of the image
        var part = apresentacao.PresentationPart.GetPartById(idImagem);
        var stream = part.GetStream();
        // Getting the image name
        var imageName = GetImageName(imageId, presentation);  
/* Here i need a method that returns the image file name based on the id of the image and the presentation object.*/
        // Setting my custom object ImageName property
        paragraph.ImageName = imageName;
        // Returning the stream
        return stream;
}

有谁知道我该怎么做? 谢谢!!

最佳答案

pptx文件中的图片/图像实际上有两个文件名:

如果您需要图像的文件名,因为它嵌入在 pptx 文件中 您可以使用以下功能:

public static string GetEmbeddedFileName(ImagePart part)
{
  return part.Uri.ToString();
}

如果您需要图像的原始文件系统名称,您可以使用以下函数:

public static string GetOriginalFileSystemName(DocumentFormat.OpenXml.Presentation.Picture pic)
{
  return pic.NonVisualPictureProperties.NonVisualDrawingProperties.Description;
}

开始编辑:

这是一个完整的代码示例:

using (var doc = PresentationDocument.Open(fileName, false))
{
  var presentation = doc.PresentationPart.Presentation;

  foreach (SlideId slide_id in presentation.SlideIdList)
  {
    SlidePart slide_part = doc.PresentationPart.GetPartById(slide_id.RelationshipId) as SlidePart;
    if (slide_part == null || slide_part.Slide == null)
        continue;
    Slide slide = slide_part.Slide;

    foreach (var pic in slide.Descendants<DocumentFormat.OpenXml.Presentation.Picture>())
    {
      string id = pic.NonVisualPictureProperties.NonVisualDrawingProperties.Id;
      string desc = pic.NonVisualPictureProperties.NonVisualDrawingProperties.Description;

      Console.Out.WriteLine(desc);
    }
  }
}

结束编辑

希望这对您有所帮助。

关于c# - 从 powerpoint 文件中检索图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7626006/

相关文章:

c# - 域模型中的域服务与实体方法

c# - LINQ 从子列表中选择

c# - 以编程方式生成的 PowerPoint 演示文稿破坏了 PowerPoint 2013

c# - Windows RT 中的自定义触摸键盘

c# - Json.Net - 如何仅在类型匹配时反序列化

c# - 如何使用 C# 和 Open XML SDK 按特定文本拆分 Word 文档?

c# - 使用 OpenXmlReader

c# - Linq 的 FirstOrDefault 充当其他类型?

c# - 使用属性在 C# 中封装数据

c# - 如何使用反射获取属性的名称和值?