I created a simple C# method with the purpose to find out if a Word Document contains an image:
public bool ContainsImage(string fileName)
{
using (var wordDocument = WordprocessingDocument.Open(fileName, false))
{
return wordDocument.MainDocumentPart.Document.Descendants<Graphic>().Any();
}
}
This works with my sample file but I am confused if it works with every docx document. I researched for solutions to handle images in Word Documents and I found a lot different approaches to detect images:
Decendants<Graphic>()
- works as describedDecendants<Inline>()
- works tooDecendants<Drawing>()
- does not workDecendants<Picture>()
- does not workWhat is the correct way? I don't care where an image is located in the document, as long it is visible. It does not matter if it is in the background, in front or between text, etc. What are the differences between these objects (Graphic, Inline, Drawing, Picture)?
By trial and error I came a shorter approach to check for the ImageParts of the different Document parts. And I do additionally check the HeaderParts and FooterParts for images now, because they were not considered before:
public bool ContainsImage(string fileName)
{
using (var wordDocument = WordprocessingDocument.Open(fileName, false))
{
if (wordDocument.MainDocumentPart.ImageParts.Any())
return true;
foreach (var header in wordDocument.MainDocumentPart.HeaderParts)
if (header.ImageParts.Any())
return true;
foreach (var footer in wordDocument.MainDocumentPart.FooterParts)
if (footer.ImageParts.Any())
return true;
}
return false;
}
I also found a solution where somebody just checked the first item of HeaderParts and FooterParts. I am not sure if this is enough and why would there be more then only the first part.