【问题标题】:OpenXML SDK remove image from PowerPoint SlideOpenXML SDK 从 PowerPoint 幻灯片中删除图像
【发布时间】:2012-08-08 13:00:45
【问题描述】:

我正在使用 OpenXML SDK 操作 Power Point 幻灯片。幻灯片上有一张图片。我想要做的是,如果在服务器上找到特定图像,则将幻灯片图像替换为在服务器上找到的图像,否则完全删除图像。

我的替换工作正常,但如果我尝试删除图像,我仍然会得到一个带有“无法显示此图像”的图像控件。

这是我删除图像的操作,注意 slidePart 是我正在操作的幻灯片:

'get the first image on the slide
Dim blip As Drawing.Blip = slidePart.Slide.Descendants(Of Drawing.Blip)().First()
blip.Remove()
slidePart.Slide.Save()

谁能告诉我我做错了什么?任何建议将不胜感激,非常感谢。

【问题讨论】:

    标签: .net vb.net openxml-sdk


    【解决方案1】:

    您想查找与您的图像对应的Picture 元素并删除该元素。我倾向于按图像名称搜索以找到 Picture 元素,然后将其删除。这是我使用的代码的 C# 示例:

    Picture imageToRemove = slidePart.Slide.Descendants<Picture>().SingleOrDefault(picture => picture.NonVisualPictureProperties.OuterXml.Contains(imageFileName));
    
    if (imageToRemove != null)
    {
        imageToRemove.Remove();
    }
    

    【讨论】:

    • 您好,感谢您的回复。幻灯片上只有一张图片,我尝试了以下操作:Dim imageToRemove As DocumentFormat.OpenXml.Presentation.Picture = slidePart.Slide.Descendants(Of DocumentFormat.OpenXml.Presentation.Picture)().SingleOrDefault() 但我没有得到任何回报。如果只有一张图片,我不应该按图片名称搜索吗?
    • Picture 元素是slidePart.Slide.CommonSlideData.ShapeTree 的子元素,因此请尝试在那里搜索该元素。如果您只有一张图片,则不必按文件名搜索。
    • 感谢您的回复。我尝试了DocumentFormat.OpenXml.Presentation.Picture = slidePart.Slide.CommonSlideData.ShapeTree.Descendants(Of DocumentFormat.OpenXml.Presentation.Picture)().SingleOrDefault(),但仍然一无所获。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多