2015-08-14 74 views
3

我正在尝试使用OpenXML在PowerPoint演示文稿中遍历图像。OpenXML - 获取图像Alt文本标题

我已经制定了如何做到这一点。

我不会试图获取图像的Alt-文字标题....

这里是我的代码:

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的专家那里,可以给我一些指点?

感谢

UPDATE:

我曾尝试通过遍历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; 
       } 
      } 
     } 
    } 
} 
+1

这里是我如何找到这样的东西出来:创建你想要什么(有替代文字的图像)的文件,然后寻找它在XML文件中。这应该给你足够的信息来弄清楚如何访问所需的信息。 – helb

回答

2

你90%那里的路。

您需要找到Blip元素,其Embed属性与您所拥有的ImagePart的ID相匹配。 Blip包含在BlipFill之内,该BlipFill又包含在Picture元素内。 (在XML pic)元素PictureNonVisualPictureDrawingProperties元素(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对象,因此您可以轻松获得其他属性(例如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); 
    } 
} 
+0

太棒了 - 现在测试...非常感谢 –

相关问题