Blog author:
Lacy | Posted on: 5/27/2012 | Category:
C# Blogs | Views: 1193 | Status:
[Member] |
Points: 75
|
Alert Moderator
Extract content from a document is always needed in people's daily work.
Now I want to share a method to extract images from a word document with C#, VB.NET. The whole process is very easy. The main method can be finished only by three steps.
How to extract images from Word file with C#, VB.NET
In my method, I use an MS word component Spire.Doc to finish this task. Spire. Doc supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight and doesn't need Microsoft Office Word Automation.
Step1. Create a new project.Create a new project in Visual Studio and set the Target framework to be .NET Framework 4.Step2. Add reference.1. Add System Drawing and Spire.Doc dll as references in Project.2. Add the below namespace at the top of the method.C# using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
using Spire.Doc.Collections;
using System.Collections;
VB.NET Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Doc.Interface
Imports Spire.Doc.Collections
Imports System.Collections
Step3. Extract images from word document.
1. Load a word document from system.C# Code: //Open a word document. Document document = new Document(@"D:\michelle\JaneEyre.doc"); int index = 0;VB.NET Code: 'Open a word document.
Dim document As New Document("D:\michelle\JaneEyre.doc ")
Dim index As Integer = 0
2. Extract images from word document
C# Code: //Create a new queue.
Queue containers = new Queue();
//Put the document objects in the queue.
containers.Enqueue(document);
while (containers.Count > 0)
{
ICompositeObject container = (ICompositeObject)containers.Dequeue();
DocumentObjectCollection docObjects = container.ChildObjects;
foreach (DocumentObject docObject in docObjects)
{
//Judge the object type.
if (docObject.DocumentObjectType == DocumentObjectType.Picture)
{
DocPicture picture = docObject as DocPicture;
//Name the image.
String imageName = String.Format("Image-{0}.png", index);
//Save the image.
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
index++;
}
else
{
if (docObject is ICompositeObject)
{
containers.Enqueue(docObject as ICompositeObject);
}
}
}
}
VB.NET Code: 'Create a new queue.
Dim containers As New Queue(Of ICompositeObject)()
'Put the document objects in the queue.
containers.Enqueue(document)
Do While containers.Count > 0
Dim container As ICompositeObject = DirectCast(containers.Dequeue(), ICompositeObject)
Dim docObjects As DocumentObjectCollection = container.ChildObjects
For Each docObject As DocumentObject In docObjects
'Judge the object type.
If docObject.DocumentObjectType = DocumentObjectType.Picture Then
Dim picture As DocPicture = TryCast(docObject, DocPicture)
'Name the image.
Dim imageName As String = String.Format("Image-{0}.png", index)
'Save the image.
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
index += 1
Else
If TypeOf docObject Is ICompositeObject Then
containers.Enqueue(TryCast(docObject, ICompositeObject))
End If
End If
Next docObject
Loop
Preview 
Word File
Extracted Images
Spire.Doc can extract images in word document easily and save the images to different commonly used formats such as png, bmp, jpg, gif, tif and so on.
Found interesting? Add this to: