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, DsWord 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)
To detect source of a paragraph's formatting properties:
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(); } |
For more information on how to modify theme colors using DsWord, see DsWord sample browser.