StyleCollection

StyleCollection class

A collection of Style objects that represent both the built-in and user-defined styles in a document.

To learn more, visit the Working with Styles and Themes documentation article.

public class StyleCollection : IEnumerable<Style>

Properties

NameDescription
Count { get; }Gets the number of styles in the collection.
DefaultFont { get; }Gets document default text formatting.
DefaultParagraphFormat { get; }Gets document default paragraph formatting.
Document { get; }Gets the owner document.
Item { get; }Gets a style by name or alias. (3 indexers)

Methods

NameDescription
Add(StyleType, string)Creates a new user defined style and adds it the collection.
AddCopy(Style)Copies a style into this collection.
ClearQuickStyleGallery()Removes all styles from the Quick Style Gallery panel.
GetEnumerator()Gets an enumerator object that will enumerate styles in the alphabetical order of their names.

Examples

Shows how to create and use a paragraph style with list formatting.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Create a custom paragraph style.
Style style = doc.Styles.Add(StyleType.Paragraph, "MyStyle1");
style.Font.Size = 24;
style.Font.Name = "Verdana";
style.ParagraphFormat.SpaceAfter = 12;

// Create a list and make sure the paragraphs that use this style will use this list.
style.ListFormat.List = doc.Lists.Add(ListTemplate.BulletDefault);
style.ListFormat.ListLevelNumber = 0;

// Apply the paragraph style to the document builder's current paragraph, and then add some text.
builder.ParagraphFormat.Style = style;
builder.Writeln("Hello World: MyStyle1, bulleted list.");

// Change the document builder's style to one that has no list formatting and write another paragraph.
builder.ParagraphFormat.Style = doc.Styles["Normal"];
builder.Writeln("Hello World: Normal.");

builder.Document.Save(ArtifactsDir + "Styles.ParagraphStyleBulletedList.docx");

See Also