AddCopy

StyleCollection.AddCopy method

Copies a style into this collection.

public Style AddCopy(Style style)
ParameterTypeDescription
styleStyleStyle to be copied.

Return Value

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 import a style from one document into a different document.

Document srcDoc = new Document();

// Create a custom style for the source document.
Style srcStyle = srcDoc.Styles.Add(StyleType.Paragraph, "MyStyle");
srcStyle.Font.Color = Color.Red;

// Import the source document's custom style into the destination document.
Document dstDoc = new Document();
Style newStyle = dstDoc.Styles.AddCopy(srcStyle);

// The imported style has an appearance identical to its source style.
Assert.AreEqual("MyStyle", newStyle.Name);
Assert.AreEqual(Color.Red.ToArgb(), newStyle.Font.Color.ToArgb());

Shows how to clone a document’s style.

Document doc = new 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".
Style newStyle = doc.Styles.AddCopy(doc.Styles["Heading 1"]);

// Use the style's "Name" property to change the style's identifying name.
newStyle.Name = "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.Font.Color = Color.Red;

Assert.AreEqual("My Heading 1", newStyle.Name);
Assert.AreEqual("Heading 1", doc.Styles["Heading 1"].Name);

Assert.AreEqual(doc.Styles["Heading 1"].Type, newStyle.Type);
Assert.AreEqual(doc.Styles["Heading 1"].Font.Name, newStyle.Font.Name);
Assert.AreEqual(doc.Styles["Heading 1"].Font.Size, newStyle.Font.Size);
Assert.AreNotEqual(doc.Styles["Heading 1"].Font.Color, newStyle.Font.Color);

See Also