Articles in this section
Category / Section

How to extract Images from Word document in C# and VB?

4 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can extract the images from Word document and save the extracted images to the desired location in C# and VB.NET.

Image is one kind of document objects which belongs to paragraph items, and we can extract the images by iterating through the paragraph items.

Steps to extract image from Word document in C#

  1. Create a new C# console application project.

Create new C# console app in WinForms

  1. Install Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.

Install WinForms NuGet packages

  1. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

VB

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
  1. Use the following code to extract image from Word document.

C#

//Loads the template document
WordDocument document = new WordDocument("Template.docx");
int index = 0;
//Visits all document entities.
foreach (var item in document.Visit())
{
      switch (item.EntityType)
      {
         // Gets the list of pictures in the Word document and save as Image
         case EntityType.Picture:
              WPicture picture = item as WPicture;
              string imageName = String.Format(@"Images/Image-{0}.jpeg", index);
              picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Jpeg);
              index++;
              break; 
       } 
}
//Closes the document
document.Close();

VB

'Loads the template document
Dim document As WordDocument = New WordDocument("Template.docx")
Dim index As Integer = 0
'Visits all document entities.
For Each item In document.Visit()
 
    Select Case item.EntityType
         'Gets the list of pictures in the Word document and save as Image
         Case EntityType.Picture
              Dim picture As WPicture = TryCast(item, WPicture)
              Dim imageName As String = String.Format("../../Images/Image-{0}.jpeg", index)
              picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Jpeg)
              index += 1
     End Select
Next
 
document.Close()
  1. Helper Class

C#

public static class DocIOExtensions
{
    public static IEnumerable<IEntity> Visit(this ICompositeEntity entity)
    {
        var entities = new Stack<IEntity>(new IEntity[] { entity });
        while (entities.Count > 0)
        {
            var e = entities.Pop();
            yield return e;
            if (e is ICompositeEntity)
            {
                foreach (IEntity childEntity in ((ICompositeEntity)e).ChildEntities)
                {
                    entities.Push(childEntity);
                }
            }
        }
    }
}

 

VB.NET

Public Module DocIOExtensions
<Extension()>
Public Iterator Function Visit(ByVal entity As ICompositeEntity) As IEnumerable(Of IEntity)
    Dim entities = New Stack(Of IEntity)(New IEntity() {entity})
    While entities.Count > 0
        Dim e = entities.Pop()
        Yield e
        If TypeOf e Is ICompositeEntity Then
            For Each childEntity As IEntity In CType(e, ICompositeEntity).ChildEntities
                entities.Push(childEntity)
            Next
        End If
    End While
End Function
End Module

 

A complete working example of extracting image from Word document in C# can be downloaded from here.

Input template Word document as follows:

Input word document

By executing the program, you will get the images in given folder as like below:

Generated images in output folder

Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

See Also:

Working with Images

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied