Detailed Description

Represents a single built-in or user-defined style.

Examples

Shows how to create and apply a style.

auto doc = MakeObject<Document>();
// Add a custom style and change its appearance
SharedPtr<Style> style = doc->get_Styles()->Add(Aspose::Words::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());
// Write a paragraph in that style
auto builder = MakeObject<DocumentBuilder>(doc);
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);
// Styles can also be removed from the collection like this
doc->get_Styles()->idx_get(u"MyStyle")->Remove();
firstParagraphStyle = doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_ParagraphFormat()->get_Style();
// Removing the style reverts the styling of the text that was in that style
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 paragraph style and specify some formatting for it
SharedPtr<Style> style = doc->get_Styles()->Add(Aspose::Words::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(Aspose::Words::Lists::ListTemplate::BulletDefault));
style->get_ListFormat()->set_ListLevelNumber(0);
// Apply the paragraph style to the current paragraph in the document and add some text
builder->get_ParagraphFormat()->set_Style(style);
builder->Writeln(u"Hello World: MyStyle1, bulleted list.");
// Change to a paragraph style that has no list formatting
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");

#include <Aspose.Words.Cpp/Model/Styles/Style.h>

+ Inheritance diagram for Aspose::Words::Style:

Public Member Functions

bool Equals (SharedPtr< Style > 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. More...
 
ArrayPtr< Stringget_Aliases ()
 Gets all aliases of this style. If style has no aliases then empty array of string is returned. More...
 
String get_BaseStyleName ()
 Gets/sets the name of the style this style is based on. More...
 
bool get_BuiltIn ()
 True if this style is one of the built-in styles in MS Word. More...
 
SharedPtr< DocumentBaseget_Document ()
 Gets the owner document. More...
 
SharedPtr< Fontget_Font ()
 Gets the character formatting of the style. More...
 
bool get_IsHeading ()
 True when the style is one of the built-in Heading styles. More...
 
bool get_IsQuickStyle () const
 Specifies whether this style is shown in the Quick Style gallery inside MS Word UI. More...
 
String get_LinkedStyleName ()
 Gets the name of the Style linked to this one. Returns Empty string if no styles are linked. More...
 
SharedPtr< Listget_List ()
 Gets the list that defines formatting of this list style. More...
 
SharedPtr< ListFormatget_ListFormat ()
 Provides access to the list formatting properties of a paragraph style. More...
 
String get_Name () const
 Gets or sets the name of the style. More...
 
String 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. More...
 
SharedPtr< ParagraphFormatget_ParagraphFormat ()
 Gets the paragraph formatting of the style. More...
 
StyleIdentifier get_StyleIdentifier () const
 Gets the locale independent style identifier for a built-in style. More...
 
SharedPtr< StyleCollectionget_Styles () const
 Gets the collection of styles this style belongs to. More...
 
StyleType get_Type () const
 Gets the style type (paragraph or character). More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void Remove ()
 Removes the specified style from the document. More...
 
void set_BaseStyleName (String value)
 Setter for get_BaseStyleName. More...
 
void set_IsQuickStyle (bool value)
 Setter for get_IsQuickStyle. More...
 
void set_Name (String value)
 Setter for get_Name. More...
 
void set_NextParagraphStyleName (String value)
 Setter for get_NextParagraphStyleName. More...
 

Static Public Member Functions

static const TypeInfoType ()
 

Member Function Documentation

◆ Equals()

bool Aspose::Words::Style::Equals ( System::SharedPtr< Aspose::Words::Style 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.

Examples

Shows how to use style aliases.

auto doc = MakeObject<Document>(MyDir + u"Style with alias.docx");
// If a style's name has multiple values separated by commas, each one is considered to be a separate alias
SharedPtr<Style> style = doc->get_Styles()->idx_get(u"MyStyle");
ASPOSE_ASSERT_EQ(MakeArray<String>({u"MyStyle Alias 1", u"MyStyle Alias 2"}), style->get_Aliases());
ASSERT_EQ(u"Title", style->get_BaseStyleName());
ASSERT_EQ(u"MyStyle Char", style->get_LinkedStyleName());
// A style can be referenced by alias as well as name
ASPOSE_ASSERT_EQ(style, doc->get_Styles()->idx_get(u"MyStyle Alias 1"));

◆ get_Aliases()

System::ArrayPtr<System::String> Aspose::Words::Style::get_Aliases ( )

Gets all aliases of this style. If style has no aliases then empty array of string is returned.

Examples

Shows how to use style aliases.

auto doc = MakeObject<Document>(MyDir + u"Style with alias.docx");
// If a style's name has multiple values separated by commas, each one is considered to be a separate alias
SharedPtr<Style> style = doc->get_Styles()->idx_get(u"MyStyle");
ASPOSE_ASSERT_EQ(MakeArray<String>({u"MyStyle Alias 1", u"MyStyle Alias 2"}), style->get_Aliases());
ASSERT_EQ(u"Title", style->get_BaseStyleName());
ASSERT_EQ(u"MyStyle Char", style->get_LinkedStyleName());
// A style can be referenced by alias as well as name
ASPOSE_ASSERT_EQ(style, doc->get_Styles()->idx_get(u"MyStyle Alias 1"));

◆ get_BaseStyleName()

System::String Aspose::Words::Style::get_BaseStyleName ( )

Gets/sets the name of the style this style is based on.

Examples

Shows how to use style aliases.

auto doc = MakeObject<Document>(MyDir + u"Style with alias.docx");
// If a style's name has multiple values separated by commas, each one is considered to be a separate alias
SharedPtr<Style> style = doc->get_Styles()->idx_get(u"MyStyle");
ASPOSE_ASSERT_EQ(MakeArray<String>({u"MyStyle Alias 1", u"MyStyle Alias 2"}), style->get_Aliases());
ASSERT_EQ(u"Title", style->get_BaseStyleName());
ASSERT_EQ(u"MyStyle Char", style->get_LinkedStyleName());
// A style can be referenced by alias as well as name
ASPOSE_ASSERT_EQ(style, doc->get_Styles()->idx_get(u"MyStyle Alias 1"));

◆ get_BuiltIn()

bool Aspose::Words::Style::get_BuiltIn ( )

True if this style is one of the built-in styles in MS Word.

Examples

Applies double underline to all runs in a document that are formatted with custom character styles.

//Document doc = new Document(MyDir + "Custom style.docx");
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Insert a custom style
SharedPtr<Aspose::Words::Style> style = doc->get_Styles()->Add(Aspose::Words::StyleType::Character, u"MyStyle");
style->get_Font()->set_Color(System::Drawing::Color::get_Red());
style->get_Font()->set_Name(u"Courier New");
// Set the style of the current paragraph to our custom style
// This will apply to only the text after the style separator
builder->get_Font()->set_StyleName(u"MyStyle");
builder->Write(u"This text is in a custom style.");
// Iterate through every run node and apply underlines to the run if its style is not a built in style,
// like the one we added
for (auto node : System::IterateOver(doc->GetChildNodes(Aspose::Words::NodeType::Run, true)))
{
auto run = System::DynamicCast<Aspose::Words::Run>(node);
SharedPtr<Aspose::Words::Style> charStyle = run->get_Font()->get_Style();
if (!charStyle->get_BuiltIn())
{
run->get_Font()->set_Underline(Aspose::Words::Underline::Double);
}
}
doc->Save(ArtifactsDir + u"Font.Style.docx");

◆ get_Document()

System::SharedPtr<Aspose::Words::DocumentBase> Aspose::Words::Style::get_Document ( )

Gets the owner document.

Examples

Shows how to access a document's style collection.

auto doc = MakeObject<Document>();
// A blank document comes with 4 styles by default
ASSERT_EQ(4, doc->get_Styles()->get_Count());
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Style>>> stylesEnum = doc->get_Styles()->GetEnumerator();
while (stylesEnum->MoveNext())
{
SharedPtr<Style> curStyle = stylesEnum->get_Current();
System::Console::WriteLine(String::Format(u"Style name:\t\"{0}\", of type \"{1}\"",curStyle->get_Name(),curStyle->get_Type()));
System::Console::WriteLine(String::Format(u"\tSubsequent style:\t{0}",curStyle->get_NextParagraphStyleName()));
System::Console::WriteLine(String::Format(u"\tIs heading:\t\t\t{0}",curStyle->get_IsHeading()));
System::Console::WriteLine(String::Format(u"\tIs QuickStyle:\t\t{0}",curStyle->get_IsQuickStyle()));
ASPOSE_ASSERT_EQ(doc, curStyle->get_Document());
}
}

◆ get_Font()

System::SharedPtr<Aspose::Words::Font> Aspose::Words::Style::get_Font ( )

Gets the character formatting of the style.

For list styles this property returns null.

Examples

Shows how to create and apply a style.

auto doc = MakeObject<Document>();
// Add a custom style and change its appearance
SharedPtr<Style> style = doc->get_Styles()->Add(Aspose::Words::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());
// Write a paragraph in that style
auto builder = MakeObject<DocumentBuilder>(doc);
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);
// Styles can also be removed from the collection like this
doc->get_Styles()->idx_get(u"MyStyle")->Remove();
firstParagraphStyle = doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_ParagraphFormat()->get_Style();
// Removing the style reverts the styling of the text that was in that style
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 paragraph style and specify some formatting for it
SharedPtr<Style> style = doc->get_Styles()->Add(Aspose::Words::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(Aspose::Words::Lists::ListTemplate::BulletDefault));
style->get_ListFormat()->set_ListLevelNumber(0);
// Apply the paragraph style to the current paragraph in the document and add some text
builder->get_ParagraphFormat()->set_Style(style);
builder->Writeln(u"Hello World: MyStyle1, bulleted list.");
// Change to a paragraph style that has no list formatting
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");

◆ get_IsHeading()

bool Aspose::Words::Style::get_IsHeading ( )

True when the style is one of the built-in Heading styles.

Examples

Shows how to access a document's style collection.

auto doc = MakeObject<Document>();
// A blank document comes with 4 styles by default
ASSERT_EQ(4, doc->get_Styles()->get_Count());
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Style>>> stylesEnum = doc->get_Styles()->GetEnumerator();
while (stylesEnum->MoveNext())
{
SharedPtr<Style> curStyle = stylesEnum->get_Current();
System::Console::WriteLine(String::Format(u"Style name:\t\"{0}\", of type \"{1}\"",curStyle->get_Name(),curStyle->get_Type()));
System::Console::WriteLine(String::Format(u"\tSubsequent style:\t{0}",curStyle->get_NextParagraphStyleName()));
System::Console::WriteLine(String::Format(u"\tIs heading:\t\t\t{0}",curStyle->get_IsHeading()));
System::Console::WriteLine(String::Format(u"\tIs QuickStyle:\t\t{0}",curStyle->get_IsQuickStyle()));
ASPOSE_ASSERT_EQ(doc, curStyle->get_Document());
}
}

◆ get_IsQuickStyle()

bool Aspose::Words::Style::get_IsQuickStyle ( ) const

Specifies whether this style is shown in the Quick Style gallery inside MS Word UI.

Examples

Shows how to access a document's style collection.

auto doc = MakeObject<Document>();
// A blank document comes with 4 styles by default
ASSERT_EQ(4, doc->get_Styles()->get_Count());
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Style>>> stylesEnum = doc->get_Styles()->GetEnumerator();
while (stylesEnum->MoveNext())
{
SharedPtr<Style> curStyle = stylesEnum->get_Current();
System::Console::WriteLine(String::Format(u"Style name:\t\"{0}\", of type \"{1}\"",curStyle->get_Name(),curStyle->get_Type()));
System::Console::WriteLine(String::Format(u"\tSubsequent style:\t{0}",curStyle->get_NextParagraphStyleName()));
System::Console::WriteLine(String::Format(u"\tIs heading:\t\t\t{0}",curStyle->get_IsHeading()));
System::Console::WriteLine(String::Format(u"\tIs QuickStyle:\t\t{0}",curStyle->get_IsQuickStyle()));
ASPOSE_ASSERT_EQ(doc, curStyle->get_Document());
}
}

◆ get_LinkedStyleName()

System::String Aspose::Words::Style::get_LinkedStyleName ( )

Gets the name of the Style linked to this one. Returns Empty string if no styles are linked.

Examples

Shows how to use style aliases.

auto doc = MakeObject<Document>(MyDir + u"Style with alias.docx");
// If a style's name has multiple values separated by commas, each one is considered to be a separate alias
SharedPtr<Style> style = doc->get_Styles()->idx_get(u"MyStyle");
ASPOSE_ASSERT_EQ(MakeArray<String>({u"MyStyle Alias 1", u"MyStyle Alias 2"}), style->get_Aliases());
ASSERT_EQ(u"Title", style->get_BaseStyleName());
ASSERT_EQ(u"MyStyle Char", style->get_LinkedStyleName());
// A style can be referenced by alias as well as name
ASPOSE_ASSERT_EQ(style, doc->get_Styles()->idx_get(u"MyStyle Alias 1"));

◆ get_List()

System::SharedPtr<Aspose::Words::Lists::List> Aspose::Words::Style::get_List ( )

Gets the list that defines formatting of this list style.

This property is only valid for list styles. For other style types this property returns null.

Examples

Shows how to create a list style and use it in a document.

auto doc = MakeObject<Document>();
// Create a new list style
// List formatting associated with this list style is default numbered
SharedPtr<Style> listStyle = doc->get_Styles()->Add(Aspose::Words::StyleType::List, u"MyListStyle");
// This list defines the formatting of the list style
// Note this list can not be used directly to apply formatting to paragraphs (see below)
SharedPtr<List> list1 = listStyle->get_List();
// Check some basic rules about the list that defines a list style
ASSERT_TRUE(list1->get_IsListStyleDefinition());
ASSERT_FALSE(list1->get_IsListStyleReference());
ASSERT_TRUE(list1->get_IsMultiLevel());
ASPOSE_ASSERT_EQ(listStyle, list1->get_Style());
// Modify formatting of the list style to our liking
for (auto level : System::IterateOver(list1->get_ListLevels()))
{
level->get_Font()->set_Name(u"Verdana");
level->get_Font()->set_Color(System::Drawing::Color::get_Blue());
level->get_Font()->set_Bold(true);
}
// Add some text to our document and use the list style
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Using list style first time:");
// This creates a list based on the list style
SharedPtr<List> list2 = doc->get_Lists()->Add(listStyle);
// Check some basic rules about the list that references a list style
ASSERT_FALSE(list2->get_IsListStyleDefinition());
ASSERT_TRUE(list2->get_IsListStyleReference());
ASPOSE_ASSERT_EQ(listStyle, list2->get_Style());
// Apply the list that references the list style
builder->get_ListFormat()->set_List(list2);
builder->Writeln(u"Item 1");
builder->Writeln(u"Item 2");
builder->get_ListFormat()->RemoveNumbers();
builder->Writeln(u"Using list style second time:");
// Create and apply another list based on the list style
SharedPtr<List> list3 = doc->get_Lists()->Add(listStyle);
builder->get_ListFormat()->set_List(list3);
builder->Writeln(u"Item 1");
builder->Writeln(u"Item 2");
builder->get_ListFormat()->RemoveNumbers();
builder->get_Document()->Save(ArtifactsDir + u"Lists.CreateAndUseListStyle.docx");

◆ get_ListFormat()

System::SharedPtr<Aspose::Words::ListFormat> Aspose::Words::Style::get_ListFormat ( )

Provides access to the list formatting properties of a paragraph style.

This property is only valid for paragraph styles. For other style types this property returns null.

Examples

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

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Create a paragraph style and specify some formatting for it
SharedPtr<Style> style = doc->get_Styles()->Add(Aspose::Words::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(Aspose::Words::Lists::ListTemplate::BulletDefault));
style->get_ListFormat()->set_ListLevelNumber(0);
// Apply the paragraph style to the current paragraph in the document and add some text
builder->get_ParagraphFormat()->set_Style(style);
builder->Writeln(u"Hello World: MyStyle1, bulleted list.");
// Change to a paragraph style that has no list formatting
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");

◆ get_Name()

System::String Aspose::Words::Style::get_Name ( ) const

Gets or sets the name of the style.

Can not be empty string.

If there already is a style with such name in the collection, then this style will override it. All affected nodes will reference new style.

Examples

Shows how to access a document's style collection.

auto doc = MakeObject<Document>();
// A blank document comes with 4 styles by default
ASSERT_EQ(4, doc->get_Styles()->get_Count());
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Style>>> stylesEnum = doc->get_Styles()->GetEnumerator();
while (stylesEnum->MoveNext())
{
SharedPtr<Style> curStyle = stylesEnum->get_Current();
System::Console::WriteLine(String::Format(u"Style name:\t\"{0}\", of type \"{1}\"",curStyle->get_Name(),curStyle->get_Type()));
System::Console::WriteLine(String::Format(u"\tSubsequent style:\t{0}",curStyle->get_NextParagraphStyleName()));
System::Console::WriteLine(String::Format(u"\tIs heading:\t\t\t{0}",curStyle->get_IsHeading()));
System::Console::WriteLine(String::Format(u"\tIs QuickStyle:\t\t{0}",curStyle->get_IsQuickStyle()));
ASPOSE_ASSERT_EQ(doc, curStyle->get_Document());
}
}

Shows how to copy a style within the same document.

auto doc = MakeObject<Document>(MyDir + u"Document.docx");
// The AddCopy method creates a copy of the specified style and automatically generates a new name for the style, such as "Heading 1_0"
SharedPtr<Style> newStyle = doc->get_Styles()->AddCopy(doc->get_Styles()->idx_get(u"Heading 1"));
// You can change the new style name if required as the Style.Name property is read-write
newStyle->set_Name(u"My Heading 1");

◆ get_NextParagraphStyleName()

System::String Aspose::Words::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.

Examples

Shows how to access a document's style collection.

auto doc = MakeObject<Document>();
// A blank document comes with 4 styles by default
ASSERT_EQ(4, doc->get_Styles()->get_Count());
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Style>>> stylesEnum = doc->get_Styles()->GetEnumerator();
while (stylesEnum->MoveNext())
{
SharedPtr<Style> curStyle = stylesEnum->get_Current();
System::Console::WriteLine(String::Format(u"Style name:\t\"{0}\", of type \"{1}\"",curStyle->get_Name(),curStyle->get_Type()));
System::Console::WriteLine(String::Format(u"\tSubsequent style:\t{0}",curStyle->get_NextParagraphStyleName()));
System::Console::WriteLine(String::Format(u"\tIs heading:\t\t\t{0}",curStyle->get_IsHeading()));
System::Console::WriteLine(String::Format(u"\tIs QuickStyle:\t\t{0}",curStyle->get_IsQuickStyle()));
ASPOSE_ASSERT_EQ(doc, curStyle->get_Document());
}
}

◆ get_ParagraphFormat()

System::SharedPtr<Aspose::Words::ParagraphFormat> Aspose::Words::Style::get_ParagraphFormat ( )

Gets the paragraph formatting of the style.

For character and list styles this property returns null.

Examples

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

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Create a paragraph style and specify some formatting for it
SharedPtr<Style> style = doc->get_Styles()->Add(Aspose::Words::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(Aspose::Words::Lists::ListTemplate::BulletDefault));
style->get_ListFormat()->set_ListLevelNumber(0);
// Apply the paragraph style to the current paragraph in the document and add some text
builder->get_ParagraphFormat()->set_Style(style);
builder->Writeln(u"Hello World: MyStyle1, bulleted list.");
// Change to a paragraph style that has no list formatting
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");

◆ get_StyleIdentifier()

Aspose::Words::StyleIdentifier Aspose::Words::Style::get_StyleIdentifier ( ) const

Gets the locale independent style identifier for a built-in style.

For user defined (custom) styles, this property returns User.

See also
Aspose::Words::Style::get_Name
Examples

Shows how to modify the position of the right tab stop in TOC related paragraphs.

auto doc = MakeObject<Document>(MyDir + u"Table of contents.docx");
// Iterate through all paragraphs formatted using the TOC result based styles; this is any style between TOC and TOC9
for (auto para : System::IterateOver(doc->GetChildNodes(Aspose::Words::NodeType::Paragraph, true)->LINQ_OfType<SharedPtr<Paragraph> >()))
{
if (para->get_ParagraphFormat()->get_Style()->get_StyleIdentifier() >= Aspose::Words::StyleIdentifier::Toc1 && para->get_ParagraphFormat()->get_Style()->get_StyleIdentifier() <= Aspose::Words::StyleIdentifier::Toc9)
{
// Get the first tab used in this paragraph, this should be the tab used to align the page numbers
SharedPtr<TabStop> tab = para->get_ParagraphFormat()->get_TabStops()->idx_get(0);
// Remove the old tab from the collection
para->get_ParagraphFormat()->get_TabStops()->RemoveByPosition(tab->get_Position());
// Insert a new tab using the same properties but at a modified position
// We could also change the separators used (dots) by passing a different Leader type
para->get_ParagraphFormat()->get_TabStops()->Add(tab->get_Position() - 50, tab->get_Alignment(), tab->get_Leader());
}
}
doc->Save(ArtifactsDir + u"Styles.ChangeTocsTabStops.docx");

◆ get_Styles()

System::SharedPtr<Aspose::Words::StyleCollection> Aspose::Words::Style::get_Styles ( ) const

Gets the collection of styles this style belongs to.

Examples

Shows how to access a document's style collection.

auto doc = MakeObject<Document>();
// A blank document comes with 4 styles by default
ASSERT_EQ(4, doc->get_Styles()->get_Count());
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Style>>> stylesEnum = doc->get_Styles()->GetEnumerator();
while (stylesEnum->MoveNext())
{
SharedPtr<Style> curStyle = stylesEnum->get_Current();
System::Console::WriteLine(String::Format(u"Style name:\t\"{0}\", of type \"{1}\"",curStyle->get_Name(),curStyle->get_Type()));
System::Console::WriteLine(String::Format(u"\tSubsequent style:\t{0}",curStyle->get_NextParagraphStyleName()));
System::Console::WriteLine(String::Format(u"\tIs heading:\t\t\t{0}",curStyle->get_IsHeading()));
System::Console::WriteLine(String::Format(u"\tIs QuickStyle:\t\t{0}",curStyle->get_IsQuickStyle()));
ASPOSE_ASSERT_EQ(doc, curStyle->get_Document());
}
}

◆ get_Type()

Aspose::Words::StyleType Aspose::Words::Style::get_Type ( ) const

Gets the style type (paragraph or character).

Examples

Shows how to access a document's style collection.

auto doc = MakeObject<Document>();
// A blank document comes with 4 styles by default
ASSERT_EQ(4, doc->get_Styles()->get_Count());
{
SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<Style>>> stylesEnum = doc->get_Styles()->GetEnumerator();
while (stylesEnum->MoveNext())
{
SharedPtr<Style> curStyle = stylesEnum->get_Current();
System::Console::WriteLine(String::Format(u"Style name:\t\"{0}\", of type \"{1}\"",curStyle->get_Name(),curStyle->get_Type()));
System::Console::WriteLine(String::Format(u"\tSubsequent style:\t{0}",curStyle->get_NextParagraphStyleName()));
System::Console::WriteLine(String::Format(u"\tIs heading:\t\t\t{0}",curStyle->get_IsHeading()));
System::Console::WriteLine(String::Format(u"\tIs QuickStyle:\t\t{0}",curStyle->get_IsQuickStyle()));
ASPOSE_ASSERT_EQ(doc, curStyle->get_Document());
}
}

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Style::GetType ( ) const
overridevirtual

Reimplemented in Aspose::Words::TableStyle.

◆ Is()

virtual bool Aspose::Words::Style::Is ( const System::TypeInfo target) const
overridevirtual

Reimplemented in Aspose::Words::TableStyle.

◆ Remove()

void Aspose::Words::Style::Remove ( )

Removes the specified style from the document.

Style removal has following effects on the document model:

  • All references to the style are removed from corresponding paragraphs, runs and tables.
  • If base style is removed its formatting is moved to child styles.
  • If style to be deleted has a linked style, then both of these are deleted.
Examples

Shows how to create and apply a style.

auto doc = MakeObject<Document>();
// Add a custom style and change its appearance
SharedPtr<Style> style = doc->get_Styles()->Add(Aspose::Words::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());
// Write a paragraph in that style
auto builder = MakeObject<DocumentBuilder>(doc);
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);
// Styles can also be removed from the collection like this
doc->get_Styles()->idx_get(u"MyStyle")->Remove();
firstParagraphStyle = doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_ParagraphFormat()->get_Style();
// Removing the style reverts the styling of the text that was in that style
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());

◆ set_BaseStyleName()

void Aspose::Words::Style::set_BaseStyleName ( System::String  value)

◆ set_IsQuickStyle()

void Aspose::Words::Style::set_IsQuickStyle ( bool  value)

◆ set_Name()

void Aspose::Words::Style::set_Name ( System::String  value)

◆ set_NextParagraphStyleName()

void Aspose::Words::Style::set_NextParagraphStyleName ( System::String  value)

◆ Type()

static const System::TypeInfo& Aspose::Words::Style::Type ( )
static
@ List
The style is a list style.
@ Toc1
@ Character
The style is a character style.
static ASPOSECPP_SHARED_API void WriteLine()
std::enable_if_t<!Details::IsIterable< Enumerable >::value, Details::EnumeratorAdapter< Enumerable, T > > IterateOver(System::SmartPtr< Enumerable > enumerable)
@ Paragraph
The style is a paragraph style.
@ Double
static ASPOSECPP_SHARED_API Color get_Blue()
@ Toc9
static ASPOSECPP_SHARED_API Color get_Navy()
static ASPOSECPP_SHARED_API Color get_Red()
static ASPOSECPP_SHARED_API const Color Empty
int ToArgb() const
@ Run
A run of text.
@ Paragraph
A paragraph of text. A Paragraph node is a container for inline level elements Run,...
@ BulletDefault
Default bulleted list with 9 levels. Bullet of the first level is a disc, bullet of the second level ...