Documents for Word, .NET Edition Documentation
Features / Document / Document Formatting Source
In This Topic
    Document Formatting Source
    In This Topic

    Usually, in a Word document, the content formatting is picked from default document style, parent content formatting or direct formatting. In order to detect the source of formatting properties, GcWord library provides the GetPropertyValueSource method in GcWordDocument class. This feature is useful for users, who want to programmatically determine the formatting source of Word objects in documents.

    The GetPropertyValueSource method takes the formatting "property" as a parameter to find and return the object in the inheritance chain which is the source of the formatting property.

    If the target property's value is determined by an object in the inheritance hierarchy, the type of the object returned by the GetPropertyValueSource method can be one of:

    If the property belongs directly to an object, that object will be returned (and it can be of any type)

    Detect Source Object of Formatting Properties

    To detect source of a paragraph's formatting properties:

    1. Create an instance of GcWordDocument and load the Word document having styles.
    2. Get a source object of the "Font.Size" property set for a run within a paragraph formatted with Heading 1 style.
      C#
      Copy Code
      public static void DetectFormatting()
      {
          //Create a new GcWordDocument 
          GcWordDocument doc = new GcWordDocument();
      
          //Get Heading 1 style
          Style style = doc.Styles[BuiltInStyleId.Heading1];
      
          //Add a run within a paragraph formatted with Heading 1 style
          doc.Body.Paragraphs.Add("text", style);
      
          //Get a source object of the "Font.Size" property from the created run
          object source = GcWordDocument.GetPropertyValueSource(() => doc.Body.Runs.First.Font.Size);
      
          //The source object must be "Heading1" style because the font size is defined here.
          Console.Write("Formatting source name: " + ((Style)source).Name);
          Console.ReadLine();
      }