Aspose::Words::StyleCollection::AddCopy method

StyleCollection::AddCopy method

Copies a style into this collection.

System::SharedPtr<Aspose::Words::Style> Aspose::Words::StyleCollection::AddCopy(const System::SharedPtr<Aspose::Words::Style> &style)
ParameterTypeDescription
styleconst System::SharedPtr<Aspose::Words::Style>&Style to be copied.

ReturnValue

Copied style ready for usage.

Remarks

Style to be copied can belong to the same document as well as to different document.

Linked style is copied.

This method does doesn’t copy base styles.

If collection already contains a style with the same name, then new name is automatically generated by adding “_number” suffix starting from 0 e.g. “Normal_0”, “Heading 1_1” etc. Use Name setter for changing the name of the imported style.

Examples

Shows how to clone a document’s style.

auto doc = MakeObject<Document>();

// 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"));

// Use the style's "Name" property to change the style's identifying name.
newStyle->set_Name(u"My Heading 1");

// Our document now has two identical looking styles with different names.
// Changing settings of one of the styles do not affect the other.
newStyle->get_Font()->set_Color(System::Drawing::Color::get_Red());

ASSERT_EQ(u"My Heading 1", newStyle->get_Name());
ASSERT_EQ(u"Heading 1", doc->get_Styles()->idx_get(u"Heading 1")->get_Name());

ASSERT_EQ(doc->get_Styles()->idx_get(u"Heading 1")->get_Type(), newStyle->get_Type());
ASSERT_EQ(doc->get_Styles()->idx_get(u"Heading 1")->get_Font()->get_Name(), newStyle->get_Font()->get_Name());
ASPOSE_ASSERT_EQ(doc->get_Styles()->idx_get(u"Heading 1")->get_Font()->get_Size(), newStyle->get_Font()->get_Size());
ASPOSE_ASSERT_NE(doc->get_Styles()->idx_get(u"Heading 1")->get_Font()->get_Color(), newStyle->get_Font()->get_Color());

Shows how to import a style from one document into a different document.

auto srcDoc = MakeObject<Document>();

// Create a custom style for the source document.
SharedPtr<Style> srcStyle = srcDoc->get_Styles()->Add(StyleType::Paragraph, u"MyStyle");
srcStyle->get_Font()->set_Color(System::Drawing::Color::get_Red());

// Import the source document's custom style into the destination document.
auto dstDoc = MakeObject<Document>();
SharedPtr<Style> newStyle = dstDoc->get_Styles()->AddCopy(srcStyle);

// The imported style has an appearance identical to its source style.
ASSERT_EQ(u"MyStyle", newStyle->get_Name());
ASSERT_EQ(System::Drawing::Color::get_Red().ToArgb(), newStyle->get_Font()->get_Color().ToArgb());

See Also