OpenXML - 获取图像替代文本标题

OpenXML - Get Image Alt Text Title

我正在尝试使用 OpenXML 循环访问 PowerPoint 演示文稿中的图像。

我已经弄清楚该怎么做了。

我正在尝试获取图像 Alt-Text 标题....

这是我的代码:

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

你已经完成了 90%。

您需要找到 Blip 元素,其 Embed 属性 与您拥有的 ImagePart 的 ID 匹配。 Blip 包含在 BlipFill 中,后者又包含在 Picture 元素中。 Picture(XML 中的pic)元素有一个 NonVisualPictureDrawingProperties 元素(nvPicPr),后者又有一个 NonVisualDrawingProperties 元素(cNvPr) 并且它在那里,您可以在其中找到标题。例如,您的 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 object,因此您也可以轻松访问其他属性(例如 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);
    }
}