How to Extract Images from Word Document in C#

This article describes how to extract images from Word document in C#. It provides the details about the environment’s configuration required for testing the sample code, step-wise procedure, and a runnable sample code that reads a DOCX file and saves each image as a PNG file. It will also provide an explanation of the classes, methods, and properties that are required to extract all images from Word document in C#.

Steps to Extract Images from Word in C#

  1. Establish the environment to add Aspose.Words for .NET to extract images
  2. Load the source Word file with images in the Document class object
  3. Get the collection of shapes from the Word document using the GetChildNodes() method
  4. Iterate through all the elements in the shapes collection
  5. Save each shape that has an image as a separate image

These steps provide details to extract pictures from Word document in C#. First, the source Word file is loaded into the Document class object, and then the shapes collection is accessed from it by using the GetChildNodes() method that takes NodeType.Shape enumerator as an argument. In the next step, this collection is iterated and each shape is tested for having an image and, if found, saved as a separate image file.

Code to Extract Images from Word File in C#

This code demonstrates the process to extract images from Word document in C# that uses the Document class object having different overload functions supporting a lot of features like setting the encoding, font settings, language preferences, and many more. Similarly, you can fetch other types of child nodes using the GetChildNodes() method like sections, header/footer, table, row, cell, and paragraphs to name a very few.

This article explains how to extract images from a Word file. If you want to learn the process to add images, refer to the article on how to add image in Word document using C#.

 English