DocumentCopyStylesFromTemplate(Document) Method

Copies styles from the specified template to a document.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.10.0

ExpandedSyntax

public void CopyStylesFromTemplate(
	Document template
)

Parameters

template
Type: Aspose.WordsDocument

ExpandedRemarks

When styles are copied from a template to a document, like-named styles in the document are redefined to match the style descriptions in the template. Unique styles from the template are copied to the document. Unique styles in the document remain intact.

ExpandedExamples

Shows how to copies styles from the template to a document via Document.
Document template = new Document(MyDir + "Rendering.docx");
Document target = new Document(MyDir + "Document.docx");

target.CopyStylesFromTemplate(template);
Shows how to copy styles from one document to another.
// Create a document, and then add styles that we will copy to another document.
Document template = new Document();

Style style = template.Styles.Add(StyleType.Paragraph, "TemplateStyle1");
style.Font.Name = "Times New Roman";
style.Font.Color = Color.Navy;

style = template.Styles.Add(StyleType.Paragraph, "TemplateStyle2");
style.Font.Name = "Arial";
style.Font.Color = Color.DeepSkyBlue;

style = template.Styles.Add(StyleType.Paragraph, "TemplateStyle3");
style.Font.Name = "Courier New";
style.Font.Color = Color.RoyalBlue;

Assert.AreEqual(7, template.Styles.Count);

// Create a document which we will copy the styles to.
Document target = new Document();

// Create a style with the same name as a style from the template document, and add it to the target document.
style = target.Styles.Add(StyleType.Paragraph, "TemplateStyle3");
style.Font.Name = "Calibri";
style.Font.Color = Color.Orange;

Assert.AreEqual(5, target.Styles.Count);

// There are two ways of calling the method to copy all the styles from one document to another.
// 1 -  Passing the template document object:
target.CopyStylesFromTemplate(template);

// Copying styles adds all styles from the template document to the target
// and overwrites existing styles with the same name.
Assert.AreEqual(7, target.Styles.Count);

Assert.AreEqual("Courier New", target.Styles["TemplateStyle3"].Font.Name);
Assert.AreEqual(Color.RoyalBlue.ToArgb(), target.Styles["TemplateStyle3"].Font.Color.ToArgb());

// 2 -  Passing the local system filename of a template document:
target.CopyStylesFromTemplate(MyDir + "Rendering.docx");

Assert.AreEqual(21, target.Styles.Count);

ExpandedSee Also