C# OpenXML-获取图像Alt文本标题
我正在尝试使用OpenXML在PowerPoint演示文稿中迭代这些图像 我已经想出了办法 我现在正在尝试获取图像Alt文本标题 这是我的密码:C# OpenXML-获取图像Alt文本标题,c#,openxml,openxml-sdk,C#,Openxml,Openxml Sdk,我正在尝试使用OpenXML在PowerPoint演示文稿中迭代这些图像 我已经想出了办法 我现在正在尝试获取图像Alt文本标题 这是我的密码: List<ImagePart> imageParts = new List<ImagePart>(); part.GetPartsOfType<ImagePart>(imageParts); foreach (ImagePart imagePart in imageParts) { if (imagePa
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
}
}
你有90%的成功率 您需要找到
Blip
元素,该元素的Embed
属性与您拥有的ImagePart
的Id匹配。Blip
包含在BlipFill
中,而后者又包含在Picture
元素中。Picture
(pic
在XML中)元素有一个NonVisualPictureDrawingProperties
元素(nvppr
),它又有一个nonvisualdrawingperties
元素(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>
下面是我如何找到这些东西的:用你想要的东西创建一个文档(一个带有alt文本的图像),然后在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>
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);
}
}