Extract Images from Word in C#, VB.NET

2010-12-28 07:50:41 Written by  Administrator
Rate this item
(0 votes)

Solution in this guide demonstrates how to extract images from an existing Word document and save them to a specified path in C# and VB.NET via Spire.Doc for .NET.

Image is one kind of document objects which belongs to paragraph items. Spire.Doc for .NET provides a DocumentObject class to store images in Document. And also provides a DocPicture class to get and set images of document. Download and Install Spire.Doc for .NET. Follow steps to extract images from Word.

  • Get each Paragraph of each Section in Document.
  • Get each DocumentObject of ChildObjects in Paragraph.
  • If the gotten DocumentObjectType is Picture, initialize a DocPicture class instance and assign the DocumentObject as value for this instance.
  • Initialize a String class instance to name extracted image instead of its original name by invoking String.Format(String format, object arg0)
  • Invoke DocPictrue.Image.Save(String, ImageFormat) method to save images.
[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
namespace ExtractImage
{

    class Program
    {
        static void Main(string[] args)
        {
            //Load document
            Document document = new Document(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");
            int index = 0;

            //Get Each Section of Document
            foreach (Section section in document.Sections)
            {
                //Get Each Paragraph of Section
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    //Get Each Document Object of Paragraph Items
                    foreach (DocumentObject docObject in paragraph.ChildObjects)
                    {
                        //If Type of Document Object is Picture, Extract.
                        if (docObject.DocumentObjectType == DocumentObjectType.Picture)
                        {
                            DocPicture picture = docObject as DocPicture;

                            //Name Image
                            String imageName = String.Format(@"images\Image-{0}.png", index);

                            //Save Image
                            picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
                            index++;
                        }
                    }
                }
            }
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace ExtractImage

	Class Program
		Private Shared Sub Main(args As String())
			'Load document
			Dim document As New Document("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")
			Dim index As Integer = 0

			'Get Each Section of Document
			For Each section As Section In document.Sections
				'Get Each Paragraph of Section
				For Each paragraph As Paragraph In section.Paragraphs
					'Get Each Document Object of Paragraph Items
					For Each docObject As DocumentObject In paragraph.ChildObjects
						'If Type of Document Object is Picture, Extract.
						If docObject.DocumentObjectType = DocumentObjectType.Picture Then
							Dim picture As DocPicture = TryCast(docObject, DocPicture)

							'Name Image
							Dim imageName As [String] = [String].Format("images\Image-{0}.png", index)

							'Save Image
							picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
							index += 1
						End If
					Next
				Next
			Next
		End Sub
	End Class
End Namespace

After debugging, all the extracted images are saved in a specified path. Open the directory, the images will be found.

Extract Word Image

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.

Additional Info

  • tutorial_title: Extract Images from Word
Last modified on Friday, 03 September 2021 03:45