how to programmatically access the builtin properties of an open xml worddoc file
Asked Answered
A

3

7

i would like to access some built in properties(like author,last modified date,etc.) 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.

Afroasian answered 13/12, 2010 at 21:48 Comment(1)
Look at this code for some nice hints: https://searchcode.com/codesearch/view/10033886/.Elsie
F
10

An explanation of the following method can be found here, but pretty much you need to pass in the properties that you want to get out of the core.xml file to this method and it will return the value:

public static string WDRetrieveCoreProperty(string docName, string propertyName)
{
   // Given a document name and a core property, retrieve the value of the property.
   // Note that because this code uses the SelectSingleNode method, 
   // the search is case sensitive. That is, looking for "Author" is not 
   // the same as looking for "author".

   const string corePropertiesSchema = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
   const string dcPropertiesSchema = "http://purl.org/dc/elements/1.1/";
   const string dcTermsPropertiesSchema = "http://purl.org/dc/terms/";

   string propertyValue = string.Empty;

   using (WordprocessingDocument wdPackage = WordprocessingDocument.Open(docName, true))
   {
      // Get the core properties part (core.xml).
      CoreFilePropertiesPart corePropertiesPart = wdPackage.CoreFilePropertiesPart;

      // Manage namespaces to perform XML XPath queries.
      NameTable nt = new NameTable();
      XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
      nsManager.AddNamespace("cp", corePropertiesSchema);
      nsManager.AddNamespace("dc", dcPropertiesSchema);
      nsManager.AddNamespace("dcterms", dcTermsPropertiesSchema);

      // Get the properties from the package.
      XmlDocument xdoc = new XmlDocument(nt);

      // Load the XML in the part into an XmlDocument instance.
      xdoc.Load(corePropertiesPart.GetStream());

      string searchString = string.Format("//cp:coreProperties/{0}", propertyName);

      XmlNode xNode = xdoc.SelectSingleNode(searchString, nsManager);
      if (!(xNode == null))
      {
         propertyValue = xNode.InnerText;
      }
   }

   return propertyValue;
}
Frijol answered 14/12, 2010 at 2:35 Comment(0)
C
5

You can also use the packaging API:

using System.IO.Packaging.Package;

[...]

using (var package = Package.Open(path))
{
    package.PackageProperties.Creator = Environment.UserName;
    package.PackageProperties.LastModifiedBy = Environment.UserName;
}

That works also for other open XML formats like power point.

Carmichael answered 20/7, 2016 at 9:33 Comment(2)
fine! i used for xlsxIntercrop
Thanks! I know it's not part of the question but to save the properties changed in this example use: package.Close();Comity
W
-1

package.Save(); Then package.closed;

I think that Is the best way.

Wording answered 23/9, 2019 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.