How to identify if the document has embeded image using openXML and C#
Home › Forums › Open-Xml-Sdk › How to identify if the document has embeded image using openXML and C#
Tagged: c#, embededimages, openxml
This topic contains 2 replies, has 3 voices, and was last updated by TapanChaudhary 4 years, 1 month ago.
-
AuthorPosts
-
July 3, 2020 at 12:26 pm #8785
Dear team
i want to check if the document has embed images in it, using C# and OpenXML
any code snippet will really helpful to me,Thanks in advnaced
PrasadOctober 1, 2020 at 12:14 pm #9715
AnonymousAlthough the documentation for OpenXML isn’t great, there is an excellent tool that you can use to see how existing Word documents are built. If you install the OpenXml SDK it comes with the DocumentReflector.exe tool under the Open XML Format SDK\V2.0\tools directory.
Images in Word documents consist of the image data and an ID that is assigned to it that is referenced in the body of the document. It seems like your problem can be broken down into two parts: finding the ID of the image in the document, and then re-writing the image data for it.
To find the ID of the image, you’ll need to parse the MainDocumentPart. Images are stored in Runs as a Drawing element
<w:p>
<w:r>
<w:drawing>
<wp:inline>
<wp:extent cx=”3200400″ cy=”704850″ /> <!– describes the size of the image –>
<wp:docPr id=”2″ name=”Picture 1″ descr=”filename.JPG” />
<a:graphic>
<a:graphicData uri=”http://schemas.openxmlformats.org/drawingml/2006/picture”>
<pic:pic>
<pic:nvPicPr>
<pic:cNvPr id=”0″ name=”filename.JPG” />
<pic:cNvPicPr />
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed=”rId5″ /> <!– this is the ID you need to find –>
<a:stretch>
<a:fillRect />
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:ext cx=”3200400″ cy=”704850″ />
</a:xfrm>
<a:prstGeom prst=”rect” />
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
</w:p>
In the above example, you need to find the ID of the image stored in the blip element. How you go about finding that is dependent on your problem, but if you know the filename of the original image you can look at the docPr element:using (WordprocessingDocument document = WordprocessingDocument.Open(“docfilename.docx”, true)) {
// go through the document and pull out the inline image elements
IEnumerable<Inline> imageElements = from run in Document.MainDocumentPart.Document.Descendants<Run>()
where run.Descendants<Inline>().First() != null
select run.Descendants<Inline>().First();// select the image that has the correct filename (chooses the first if there are many)
Inline selectedImage = (from image in imageElements
where (image.DocProperties != null &&
image.DocProperties.Equals(“image filename”))
select image).First();// get the ID from the inline element
string imageId = “default value”;
Blip blipElement = selectedImage.Descendants<Blip>().First();
if (blipElement != null) {
imageId = blipElement.Embed.Value;
}
}
Then when you have the image ID, you can use that to rewrite the image data. I think this is how you would do it:ImagePart imagePart = (ImagePart)document.MainDocumentPart.GetPartById(imageId);
byte[] imageBytes = File.ReadAllBytes(“new_image.jpg”);
BinaryWriter writer = new BinaryWriter(imagePart.GetStream());
writer.Write(imageBytes);
writer.Close();October 4, 2020 at 4:56 am #9728UClean is a leading cleaning company that provides the best cleaning services such as laundry service, dry cleaning service, and home cleaning service at affordable prices. Contact UClean today for a cleaning service.
-
AuthorPosts
You must be logged in to reply to this topic.