我正在浏览一个docx,使用open xml来查找一些图像。
那么,我这样做是为了获取所有的绘图,它们的子文档属性,标题包含
List<Drawing> sdtElementDrawing = wordDoc.MainDocumentPart.Document.Descendants<Drawing>()
.Where(element =>
element.GetFirstChild<Drawing>() != null
&& element.GetFirstChild<DocProperties>().Title.Value.Contains("IMAGE")).ToList();
作为xml的docx在第一部分中看起来像这样(它通常更大,但我只复制相关部分):
<w:drawing>
<wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251658240" behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
<wp:simplePos x="0" y="0" />
<wp:positionH relativeFrom="column">
<wp:posOffset>2171700</wp:posOffset>
</wp:positionH>
<wp:positionV relativeFrom="paragraph">
<wp:posOffset>-1168400</wp:posOffset>
</wp:positionV>
<wp:extent cx="2286000" cy="746760" />
<wp:effectExtent l="0" t="0" r="0" b="0" />
<wp:wrapNone />
<wp:docPr id="1" name="Image 1" descr="C:UsersPicturesIMAGERM.jpg" title="IMAGERM" />
</wp:anchor>
</w:drawing>
但是我没有找到sdtElementDrawing
。
所以我认为我写错了sdtElementDrawing
查询。我必须错过一个明显的方式来告诉"得到绘图子,那里有一个标题像图像的DocProperties",但我找不到它。
我也试过这个,但没有更好的工作:
List<Drawing> sdtElementDrawing = wordDoc.MainDocumentPart.Document.Descendants<Drawing>()
.Where(element =>
element.GetFirstChild<Drawing>() != null &&
element.GetFirstChild<Drawing>().Anchor.GetFirstChild<DocProperties>().Title.Value.Contains("IMAGE")).ToList();
你试过这个吗?
List<Drawing> sdtElementDrawing = wordDoc.MainDocumentPart.Document.Descendants<Drawing>()
.Where(element => element.Descendants<DocProperties>().Any(
prop => prop.Title.Value.ToUpper().Contains("IMAGE")
)).ToList();