从 powerpoint 文件中检索图像名称

发布于 2024-12-07 12:17:37 字数 975 浏览 1 评论 0原文

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

有人知道我如何完成此操作吗? 谢谢!!

I need to retrieve the image file name from an image in a pptx file. I already got the stream from the image but i didn't got the name of the image file... Here is my code:

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

Anyone knows how i can accomplish this ?
Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2024-12-14 12:17:37

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

BEGIN EDIT:

以下是完整的代码示例:

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

END EDIT >

希望这会有所帮助。

There are in fact two file names for a picture/image in a pptx file:

If you need the file name of the image as it is embedded in the pptx file
you can use the following function:

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

If you need the orignial file system name of your image you can use the following function:

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

BEGIN EDIT:

Here is a complete code example:

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

END EDIT

Hope, this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文