【问题标题】:Retrieving the image name from a powerpoint file从 powerpoint 文件中检索图像名称
【发布时间】:2011-10-02 11:15:39
【问题描述】:

我需要从 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;
}

任何人都知道我怎么能做到这一点? 谢谢!!

【问题讨论】:

    标签: c# .net openxml-sdk presentation


    【解决方案1】:

    实际上,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);
        }
      }
    }
    

    结束编辑

    希望,这会有所帮助。

    【讨论】:

    • 嗨,感谢您的回答,但属性 NonVisualDrawingProperties 并不直接存在于 Picture 对象中...首先我需要检索 NonVisualPictureProperties 然后是 NonVisualDrawingProperties.Description,但是当我访问此属性时,她返回null... 之前我需要输入一些 using 或其他内容?
    • @mnatan.brito:你是对的。 NonVisualDrawingProperties 属性不直接存在于图片类中。我用更完整的代码示例更新了我的答案。我再次运行了一个 power point 2007 文档并打印了原始文件名。希望,这会有所帮助。
    • @mnatan.brito:另一种选择是将您的 file.pptx 重命名为 file.zip。然后打开 zip 存档并在存档中包含的所有文件中搜索嵌入图像之一的原始文件名。如果您没有找到,那么您的 pptx 不包含原始文件名信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2011-05-09
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多