Tre*_*iel 5 c# openxml openxml-sdk
我正在尝试使用 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
}
}
Run Code Online (Sandbox Code Playgroud)
有没有 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;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你已经完成了 90% 的工作。
您需要找到Blip其Embed属性与ImagePart您拥有的 Id 匹配的元素。在Blip被包含内的BlipFill其又包含一个内Picture元件。将Picture(pic在XML)元素有一个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>
Run Code Online (Sandbox Code Playgroud)
以下代码将输出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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1590 次 |
| 最近记录: |