How to Add Metadata to MS Word Document In C#

We can use DSOFile.dll to add metadata to MS Word Document or any other type MS Documents such as MS Excel, MS Powerpoint  and Images. Not only standard meta data such as created date, author, subject, category it also supports to add custom meta data.


You can download  Microsoft Developer Support OLE File Property Reader 2.1 Sample (KB 224351) and get the DSOFile.dll from here


Setup DSOFile.dll


Create a new project in visual studio.
File -> New ->Project -> select console application in 'New Project' wizard.
Then Give suitable name and browse particular location to save project.
then select References from Solution Explorer and Right Click on it.
Select 'Add Reference' and select COM-> DSO OLE Document Properties Reader 2.1, click 'ok'




Create the source code to add meta data


Select on the Project name in solution explorer and right click on it.
Add -> New Item -> Code File and give suitable name.
Then add this code to it.

DSOFile.OleDocumentPropertiesClass document =  new DSOFile.OleDocumentPropertiesClass();
document.Open(FileName, false, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
DSOFile.SummaryProperties summProps =  document.SummaryProperties;
DSOFile.CustomProperties customProperties = document.CustomProperties;

//add summary properties using DSOFile.SummaryProperties reference.

summProps.Title = "Meta Data";
summProps.Subject = "Test Meta Data";
summProps.LastSavedBy = "Nadz";
summProps.Comments = "This is testing document to add metadata";

//add custom meta data using DSOFile.CustomProperties reference

            Object email = "nadz.ds86@gmail.com";
            customProperties.Add("Email", ref email);


            Object contactNo = "0112345678";
            customProperties.Add("ContactNo", ref contactNo);


            Object position = "SE";
            customProperties.Add("Position", ref position);


            Object department = "IT department";
            customProperties.Add("Department", ref department);

//save document

            document.Save();

It may be useful to know about getting Custom Metadata from MS Document or Image

            customProperties = document.CustomProperties;

            IEnumerator ienum = customProperties.GetEnumerator();
            int count = 0;

            while (ienum.MoveNext())
            {

                Type type = ienum.Current.GetType();
                       
                 string strValue = type.InvokeMember("Value",
                                            System.Reflection.BindingFlags.Default |
                                            System.Reflection.BindingFlags.GetProperty,
                                            null, ienum.Current,
                                            new object[] { }).ToString();
               

                 Console.WriteLine("value : " + strValue); 


//Get Document Icon and Display it on Label in WPF Form


             Object image_icon = document.Icon;
            string itype = image_icon.GetType().InvokeMember("Type",               System.Reflection.BindingFlags.GetProperty, null, image_icon, new object[] { }).ToString();
            Console.WriteLine("Int type :" + itype);
            if (itype.Equals("3")) {

                Object iHandle = image_icon.GetType().InvokeMember("Handle", System.Reflection.BindingFlags.GetProperty, null, image_icon, new object[] { });
                System.IntPtr intptr = new IntPtr(Convert.ToInt32(iHandle));
                

System.IO.MemoryStream ms = new System.IO.MemoryStream();

                   System.Drawing.Bitmap bitmap =    System.Drawing.Bitmap.FromHicon(metaInfo.getIntPtr());

                   bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                   BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
                   bImg.BeginInit();
                   bImg.StreamSource = new System.IO.MemoryStream(ms.ToArray());
                   bImg.CreateOptions = BitmapCreateOptions.None;
                   bImg.CacheOption = BitmapCacheOption.Default;
                  
                  bImg.EndInit();

                  ms.Close();
                  label1.Source =  bImg;
                          }

  

Comments

  1. Nice Article !!!

    Can you please tell me how to display the custom properties of the word documents(2003,2007 and 2010) formats using DSO file.

    Kind Regards,
    Maruthu

    ReplyDelete
  2. OMG Nice article dude. I am using ur first method that i s using OLE DB. But i am having an exception error on the code below .
    DSOFile.OleDocumentProperties docword = new DSOFile.OleDocumentProperties();


    it says file not found exception:{Retrieving the COM class factory for component with CLSID {58968145-CF05-4341-995F-2EE093F6ABA3} failed due to the following error: 8007007e}


    Please hrlp me dude. neeed your reply as fast as possible.

    ReplyDelete
    Replies
    1. hi,
      I am also facing the same issue ? Is there any fix for it

      Please help me!! need you reply ASAP

      Thanks!!!

      Delete
  3. In other words, an Office 365 migration that is stress-free! These objectives can be achieved by following these 7 steps: office 365

    ReplyDelete

Post a Comment

Popular posts from this blog

How to add standard and custom Metadata to PDF using iTextSharp

How to set and get Metadata to image using BitmapMetadata class in C#

How to generate direct line token to start a new conversation between your own client application and bot framework in Java