Aspose::Words::Style class

Style class

Represents a single built-in or user-defined style. To learn more, visit the Working with Styles and Themes documentation article.

class Style : public Aspose::Words::IParaAttrSource,
              public Aspose::Words::IRunAttrSource

Methods

MethodDescription
Equals(const System::SharedPtr<Aspose::Words::Style>&)Compares with the specified style. Styles Istds are compared for built-in styles only. Styles defaults are not included in comparison. Base style, linked style and next paragraph style are recursively compared.
get_Aliases()Gets all aliases of this style. If style has no aliases then empty array of string is returned.
get_AutomaticallyUpdate() constSpecifies whether this style is automatically redefined based on the appropriate value.
get_BaseStyleName()Gets/sets the name of the style this style is based on.
get_BuiltIn()True if this style is one of the built-in styles in MS Word.
get_Document()Gets the owner document.
get_Font()Gets the character formatting of the style.
get_IsHeading()True when the style is one of the built-in Heading styles.
get_IsQuickStyle() constSpecifies whether this style is shown in the Quick Style gallery inside MS Word UI.
get_LinkedStyleName()Gets the name of the Style linked to this one. Returns empty string if no styles are linked.
get_List()Gets the list that defines formatting of this list style.
get_ListFormat()Provides access to the list formatting properties of a paragraph style.
get_Locked() const
get_Name() constGets or sets the name of the style.
get_NextParagraphStyleName()Gets/sets the name of the style to be applied automatically to a new paragraph inserted after a paragraph formatted with the specified style.
get_ParagraphFormat()Gets the paragraph formatting of the style.
get_Priority() const
get_SemiHidden() const
get_StyleIdentifier() constGets the locale independent style identifier for a built-in style.
get_Styles() constGets the collection of styles this style belongs to.
get_Type() constGets the style type (paragraph or character).
get_UnhideWhenUsed() const
GetType() const override
Is(const System::TypeInfo&) const override
Remove()Removes the specified style from the document.
set_AutomaticallyUpdate(bool)Setter for Aspose::Words::Style::get_AutomaticallyUpdate.
set_BaseStyleName(const System::String&)Setter for Aspose::Words::Style::get_BaseStyleName.
set_IsQuickStyle(bool)Setter for Aspose::Words::Style::get_IsQuickStyle.
set_Locked(bool)
set_Name(const System::String&)Setter for Aspose::Words::Style::get_Name.
set_NextParagraphStyleName(const System::String&)Setter for Aspose::Words::Style::get_NextParagraphStyleName.
set_Priority(int32_t)
set_SemiHidden(bool)
set_UnhideWhenUsed(bool)
static Type()

Examples

Shows how to create and apply a custom style.

auto doc = MakeObject<Document>();

SharedPtr<Style> style = doc->get_Styles()->Add(StyleType::Paragraph, u"MyStyle");
style->get_Font()->set_Name(u"Times New Roman");
style->get_Font()->set_Size(16);
style->get_Font()->set_Color(System::Drawing::Color::get_Navy());

auto builder = MakeObject<DocumentBuilder>(doc);

// Apply one of the styles from the document to the paragraph that the document builder is creating.
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"MyStyle"));
builder->Writeln(u"Hello world!");

SharedPtr<Style> firstParagraphStyle = doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_ParagraphFormat()->get_Style();

ASPOSE_ASSERT_EQ(style, firstParagraphStyle);

// Remove our custom style from the document's styles collection.
doc->get_Styles()->idx_get(u"MyStyle")->Remove();

firstParagraphStyle = doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_ParagraphFormat()->get_Style();

// Any text that used a removed style reverts to the default formatting.
ASSERT_FALSE(doc->get_Styles()->LINQ_Any([](SharedPtr<Style> s) { return s->get_Name() == u"MyStyle"; }));
ASSERT_EQ(u"Times New Roman", firstParagraphStyle->get_Font()->get_Name());
ASPOSE_ASSERT_EQ(12.0, firstParagraphStyle->get_Font()->get_Size());
ASSERT_EQ(System::Drawing::Color::Empty.ToArgb(), firstParagraphStyle->get_Font()->get_Color().ToArgb());

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

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

// Create a custom paragraph style.
SharedPtr<Style> style = doc->get_Styles()->Add(StyleType::Paragraph, u"MyStyle1");
style->get_Font()->set_Size(24);
style->get_Font()->set_Name(u"Verdana");
style->get_ParagraphFormat()->set_SpaceAfter(12);

// Create a list and make sure the paragraphs that use this style will use this list.
style->get_ListFormat()->set_List(doc->get_Lists()->Add(ListTemplate::BulletDefault));
style->get_ListFormat()->set_ListLevelNumber(0);

// Apply the paragraph style to the document builder's current paragraph, and then add some text.
builder->get_ParagraphFormat()->set_Style(style);
builder->Writeln(u"Hello World: MyStyle1, bulleted list.");

// Change the document builder's style to one that has no list formatting and write another paragraph.
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Normal"));
builder->Writeln(u"Hello World: Normal.");

builder->get_Document()->Save(ArtifactsDir + u"Styles.ParagraphStyleBulletedList.docx");

See Also