Extract image from word files


Hi all, I had an requirement to extract images from word files and save it in an folder in ‘jpeg’ format

 
My approach went like this, to make end user easy to work with i made this simple design.
Microsoft.Office.Interop.Word is the main assembly which will be available if MS Office Word is installed.(for developement,
in case of deployment it can be packed with the product)
 
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass ();
Document doc = wordApp.Documents.Open(ref str1, refmissing, ref yes, ref missing, ref missing, refmissing, ref missing, ref issing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
object wordApp.Visible = false ;
str1 = str;
object missing = System.Reflection. Missing.Value;
object yes = true;
 

 

 

//////////////////////Check whether the word file contains images and proceed to extraction 

 if (doc.InlineShapes.Count > 0)
                    {                    
                        for (int j = 0; j < doc.InlineShapes.Count; j++)
                        {
                            doc.InlineShapes[j].Select();
                            doc.ActiveWindow.Selection.CopyAsPicture();                          
                            IDataObject data = Clipboard.GetDataObject();
                            if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
                            {
                                object bm = data.GetData(DataFormats.Bitmap);
                                Bitmap bmp;
                                bmp = (Bitmap)data.GetData(typeof(System.Drawing.Bitmap));
                                strcopy.Remove(strcopy.LastIndexOf(“.”));
                                tail = strcopy.Substring(strcopy.LastIndexOf(“\\”) + 1, strcopy.Length – strcopy.LastIndexOf(“\\”) – 5);
                                str.Remove(pos);
                                path = str.Remove(pos) + “\\Extractedimages” + tail;
                                System.IO.Directory.CreateDirectory(path);
                                tail = path;
                                path = path + “\\” + j.ToString() + “.bmp”;
                                bmp.Save(path);
                             
                            }
                        }
                        MessageBox.Show(“images are saved in the folder” + tail);
                        wordApp.Quit(ref missing, ref missing, ref missing);                     
                    }
  


And hurray you are done.The images will be saved in the same location inside a folder with prefix ‘Extracted images’ and the file name.
Advertisements

3 responses to “Extract image from word files

  1. This code doesn’t even come close to compiling.

  2. This is developed as an product and works fine.Check the office library version(11.0).Any type of image stored in word file can be extacted as bitmap image file

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