Question

stazera on Mon, 13 Dec 2010 21:44:45


i would like to display some built in properties of an open xml word doc file. i would like to use open xml sdk2.0 for this purpose. so i wonder if there is any class or any way i could programmatically access these builtin properties.

Sponsored



Replies

Vijay Srinivasan on Tue, 14 Dec 2010 16:31:13


Hi Stazera,

Herewith i have placed the code to retrieve the Built-in document properties from a word document using Open Xml SDK. Add exception handler properly to avoid exception when the property was not present in the word document. Let me know if this helps.



   WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filePath, true);
            Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
            String message = string.Empty;
            
            ExtendedFilePropertiesPart appPart = wordprocessingDocument.ExtendedFilePropertiesPart;
            Manager = appPart.Properties.Manager.InnerText;
            if (Manager != null)
                message = "Manager : " + Manager + "\n";

            //Access company property
            Company = appPart.Properties.Company.Text;
            if (Company != null)
                message += "Company : " + Company + "\n";

            CoreFilePropertiesPart corePart = wordprocessingDocument.CoreFilePropertiesPart;
            XmlDocument xmlProperties = new XmlDocument();
            xmlProperties.Load(corePart.GetStream());

            //Access Title property
            Title = xmlProperties.GetElementsByTagName("title",DC).Item(0).InnerText;
            if (Title != null)
                message += "Title : " + Title + "\n";

            //Access Subject property
            Subject = xmlProperties.GetElementsByTagName("subject", DC).Item(0).InnerText;
            if (Subject != null)
                message += "Subject : " + Subject + "\n";

            //Access Author property
            Author = xmlProperties.GetElementsByTagName("creator", DC).Item(0).InnerText;
            if (Author != null)
                message += "Author : " + Author + "\n";

            //Access Keywords property
            KeyWords = xmlProperties.GetElementsByTagName("keywords", CP).Item(0).InnerText;
            if (KeyWords != null)
                message += "KeyWords : " + KeyWords + "\n";

            //Access Comments property
            Comments = xmlProperties.GetElementsByTagName("description", DC).Item(0).InnerText;
            if (Comments != null)
                message += "Comments : " + Comments + "\n";

            //Access Category property
            Category = xmlProperties.GetElementsByTagName("category", CP).Item(0).InnerText;
            if (Category != null)
                message += "Category : " + Category + "\n";

            MessageBox.Show(message, "Built-In Properties", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            wordprocessingDocument.Close();





Regards,
Vijay

stazera on Tue, 14 Dec 2010 18:47:28


thanks a lot vijay. that really helped

jasonsuniquedisplayname on Wed, 22 Dec 2010 20:07:08


Thank you for posting this, it has helped me a lot. I have a couple questions, sorry if they are elementary I am just learning. 

 

1) I am getting "A first chance exception of type 'System.IO.FileFormatException' occurred in WindowsBase.dll" using  WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filePath, true);. I am using Windows 7 x86 and Office 2010 attempting to open a Word 2010 document.  IDE is VS 2010 Ultimate. 

 

2) What do DP and CS represent in  the code (Title = xmlProperties.GetElementsByTagName("title",DC).Item(0).InnerText; and similar)?. I am not able to use those in my code. 

 

Thank you.

 

Jason

Vijay Srinivasan on Thu, 23 Dec 2010 05:01:21


Jason,

<b> Regarding System.IO.FileFormatException: </b>
The Chance for the occurence of this exception is only if the input word document is an invalid docx file. In some cases like, the document was initially saved as *.doc (Word 97 - 2003) format and then the extension along was changed to docx file, will also cause this exception.

Is this exception occurs only with a particular document or with all the documents? If this issue is with a particular document, can you try to resave the document once by MS Word and try with open xml.

<b> Regarding DP and CS in the code: </b>

These are nothing but the constant which specifies the namespace of the particular tag.
private const string AP = @"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
private const string DC = @"http://purl.org/dc/elements/1.1/";
private const string CP = @"http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
Let me know if this helps.

raymilam on Thu, 10 Nov 2016 15:55:31


this answer helped me a lot.

I will complete this answer putting how change a property value:

        private static void AtualizarPropriedadesDoDocumento(WordprocessingDocument document2)
        {
            ExtendedFilePropertiesPart appPart = document2.ExtendedFilePropertiesPart;
            if (appPart.Properties.Company != null)
            {
                appPart.Properties.Company.Text = "TestIT";
                appPart.Properties.Save();
            }

            CoreFilePropertiesPart corePart = document2.CoreFilePropertiesPart;
            XmlDocument xmlProperties = new XmlDocument();
            xmlProperties.Load(corePart.GetStream());

            xmlProperties.GetElementsByTagName("title", DC).Item(0).InnerText = "Test Title";
            xmlProperties.GetElementsByTagName("description", DC).Item(0).InnerText = "Test Description";

            xmlProperties.Save(corePart.GetStream());
        }