【问题标题】:OpenXML - Get Image Alt Text TitleOpenXML - 获取图像替代文本标题
【发布时间】:2021-01-29 13:39:28
【问题描述】:

我正在尝试使用 OpenXML 遍历 PowerPoint 演示文稿中的图像。

我已经想出了如何做到这一点。

我现在正在尝试获取图像替代文本标题....

这是我的代码:

List<ImagePart> imageParts = new List<ImagePart>();

part.GetPartsOfType<ImagePart>(imageParts);

foreach (ImagePart imagePart in imageParts)
{
    if (imagePart != null)
    {

    // Get the Relationship Id
    string oldRelID = part.GetIdOfPart(imagePart);

    // Get the Alt-Text Tile relating to this image

    }
}

有没有 OpenXML 专家可以给我一些指点?

谢谢

更新:

我已经尝试遍历 XML,但是当幻灯片上有多个图像时,我得到的相关图像的标题值不正确。

我想我需要能够使用 ImagePart Id 来找到相应的标题

下面的代码使标题乱序...

foreach (ImagePart imagePart in imageParts)
{
    string mapReference = "";

    XmlNode thisNode = pictureNodeList[imageCounter];

    foreach (XmlNode xmlnode in thisNode)
    {
        foreach (XmlNode xmlchildnode in xmlnode)
        {
            foreach (XmlAttribute att in xmlchildnode.Attributes)
            {
                if (att.Name == "title")
                {
                    mapReference = att.Value;
                    imageCounter += 1;
                 }
            }
         }
    }
}

【问题讨论】:

  • 这是我如何找到这些东西的方法:使用您想要的内容创建一个文档(带有替代文本的图像),然后在 XML 文件中查找它。这应该为您提供了足够的信息来弄清楚如何访问所需的信息。

标签: c# openxml openxml-sdk


【解决方案1】:

您已经完成了 90% 的工作。

您需要找到Blip 元素的Embed 属性与您拥有的ImagePart 的ID 匹配。 Blip 包含在 BlipFill 中,而 BlipFill 又包含在 Picture 元素中。 Picture(XML 中的pic)元素有一个NonVisualPictureDrawingProperties 元素(nvPicPr),而NonVisualDrawingProperties 元素(cNvPr)位于there 中你会找到标题。例如,您的 XML 可能如下所示:

<p:pic>
    <p:nvPicPr>
       <p:cNvPr id="4" name="Picture 3" descr="My Description" title="My Title" />
       <p:cNvPicPr>
          <a:picLocks noChangeAspect="1" />
       </p:cNvPicPr>
       <p:nvPr />
    </p:nvPicPr>
    <p:blipFill>
       <a:blip r:embed="rId2" cstate="print">
          <a:extLst>
             <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
             </a:ext>
          </a:extLst>
       </a:blip>
       <a:stretch>
          <a:fillRect />
       </a:stretch>
    </p:blipFill>
    <p:spPr>
       <a:xfrm>
          <a:off x="7260298" y="5445224" />
          <a:ext cx="1883701" cy="1412776" />
       </a:xfrm>
       <a:prstGeom prst="rect">
          <a:avLst />
       </a:prstGeom>
    </p:spPr>
 </p:pic>

以下代码将输出每个图像的Title 属性。请注意,由于您有一个强类型 Picture 对象,因此您也可以轻松访问其他属性(例如 Description)。

using (PresentationDocument doc =
            PresentationDocument.Open(filename, false))
{
    //get the first slide
    SlidePart part = doc.PresentationPart.SlideParts.First();

    //get all ImageParts in the first slide
    List<ImagePart> imageParts = new List<ImagePart>();
    part.GetPartsOfType<ImagePart>(imageParts);

    foreach (ImagePart imagePart in imageParts)
    {
        //find the picture related to the image
        Picture pic = part.Slide.Descendants<Picture>().Where(p => 
                        p.BlipFill.Blip.Embed == part.GetIdOfPart(imagePart)).FirstOrDefault();

        //Output the Title property
        Console.WriteLine(pic.NonVisualPictureProperties.NonVisualDrawingProperties.Title);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多