How to Extract Image from Word Document with Silverlight

Actually, we may feel a little boring if a Word document is filled with text without any other elements. Therefore, in order to make document more appealed, document owner will insert images which are related to contents, for example, people may insert actor photo in a document which focuses on introducing film.

Although the image is used to decorate one document, people would like to save it to use in other document. At that time, we need to extract image from document. In this post, I will share the method to extract image from one Word document with Silverlight. I will extract images from the prepared Word document and then save one of the extracted images.

Note: in order to realize this function more easily and quickly, I have used a component, Spire.Doc for Silverlight. If you want to use the following code, please DOWNLOAD and install this component. Then, add its dll file as reference in project.

STEPS

Step 1. Design UserControl

Rename MainPage.xaml as Extract.xaml. Double click it to design UserControl. Firstly, adjust size of UserControl and then add a background image for it. Secondly, add a label. Change label content and set content format, including font style and color. Thirdly, add a button to run and set button format.

Step 2. Declare SaveFileDialog

Declare a SaveFileDialog to save the image which will be extracted from Word. And set a filter for this SaveFileDialog to choose image format. I set the format as .png only.

Step 3. Load Document

Right click project name to add existed item which is the Word document. After adding, change the document Build Action as Embedded Resource.

Double click run button to write code. Declare document at first. Then, use foreach sentence to get name from assembly. If the name is the same with embedded resource name, load this document.

Step 4. Extract Image

Because there may be several images in document, so before extracting, we need to set image number index.

Next, use foreach sentence to get each section in document. In this loop, use a foreach sentence to get each paragraph in section then use the third foreach sentence to get document object in paragraph. If the object type is image, extract it. Because there are several images, so set a format for image name.

Step 5. Save Image

Judge if the SaveFileDialog can pop up. If the result is true, save image through stream.Write() method. There are three parameters passed to this method, buffer, offset and count.

Full Extract.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Reflection;

using System.IO;

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

 

namespace ExtractImage

{

    public partial class MainPage : UserControl

    {

        //Declare SaveFileDialog

        private SaveFileDialog saveFiledialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

            this.saveFiledialog.Filter = “Image (*.png)|*.png”;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            //Load Document

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith(“New Zealand.docx”))

                {

                    using (Stream docStream = assembly.GetManifestResourceStream(name))

                    {

                        document = new Document(docStream, FileFormat.Docx);

                    }

                }

            }

 

            //Set Image Number Index

            int index = 0;

 

            //Extract Image

            foreach (Section section in document.Sections)

            {

                foreach (Paragraph paragraph in section.Paragraphs)

                {

                    foreach (DocumentObject docObject in paragraph.ChildObjects)

                    {

                        if (docObject.DocumentObjectType == DocumentObjectType.Picture)

                        {

                            DocPicture picture = docObject as DocPicture;

                            String imageName = String.Format(“Image-{0}.png”, index);                

                            index++;

 

                            //Save Image

                            bool? result = this.saveFiledialog.ShowDialog();

                            if (result.HasValue && result.Value)

                            {

                                using (Stream stream = this.saveFiledialog.OpenFile())

                                {

                                    stream.Write(picture.ImageBytes, 0, picture.ImageBytes.Length);

                                }

                            }

                        }

                    }

                }

            }

        }

    }

}

Result

 _____________________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s