# Styles

DsWord provides you the ability to style a Word document using built-in styles. Learn how to add, delete, and modify document styles in a Word document.

## Content

Document styles are the pre-defined set of formatting instructions which can be re-used any number of times in a document. For instance, a document style once defined for a list can be used to represent all other similar lists in the document.
DsWord provides you the ability to style a Word document using built-in styles. It offers different style types through [BuiltInStyleId](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.BuiltInStyleId.html) enumeration which can be applied to the corresponding content type. In addition to the built-in styles, DsWord also allows you to define styles on your own using the [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Style.html) class. You can add a defined style to the style collection using [Add](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.StyleCollection.Add.html) method of the [StyleCollection](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.StyleCollection.html) class and the style collection can be accessed using [Styles](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Run.Style.html) property of the [GcWordDocument](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.GcWordDocument.html) class.

## Create Document Styles

To create new style for a Word document:

1. Define a new style using the **Style** class and add it to the document using the **Add** method.
2. Access the content object on which a style has to be applied. For example, access first text run from the first paragraph.
3. Apply the defined style on the text run using [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Run.Style.html) property of the [Run](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Run.html) class.

    ```csharp
    doc.Load("SampleDoc.docx");
    
    //Access the first paragraph
    Paragraph p = doc.Body.Sections.First.GetRange().Paragraphs.First;
    
    //Access the text in the first run
    Run run = p.GetRange().Runs.First;
    
    // Create a new char style "style1" for the first half
    Style style1 = doc.Styles.Add("Style1", StyleType.Character);
    style1.Font.Name = "Times New Roman";
    style1.Font.Size = 16;
    style1.Font.Bold = true;
    style1.Font.Italic = true;
    style1.Font.Color.RGB = Color.Blue;
    style1.Font.Underline = Underline.Thick;
    
    //Apply style to the text run
    run.Style = style1;
    
    //Save the document
    doc.Save("StyleAdded.docx");
    ```

## Modify Document Styles

To modify document style:

1. Access the content object on which a style is applied. For example, access first text run of the first paragraph.
2. Modify the applied styles on the text run. For example, font name and color.
3. Apply the modified style on the text run using **Style** property of the **Run** class.

    ```csharp
    doc.Load("StyleAdded.docx");
    
    //Access the first paragraph
    Paragraph para1 = doc.Body.Sections.First.GetRange().Paragraphs.First;
    
    //Access the first run
    Run run = para1.GetRange().Runs.First;
    
    //Modify the existing style's font name and color
    Style s1 = run.Style;
    s1.Font.Color.RGB = Color.Brown;
    s1.Font.Name = "Arial";
    
    //Apply style to the run
    run.Style = s1;
    
    //Save the document
    doc.Save("ModifyStyles.docx");
    ```

## Delete Document Styles

To delete the style applied on a Word document:

1. Access the content object on which a style is applied. For example, access first text run of the first paragraph.
2. Delete the style applied on the accessed range using [Delete](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Style.Delete.html) method of the [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Style.html) class.

    ```csharp
    doc.Load("StyleAdded.docx");
    
    //Access the first paragraph
    Paragraph p = doc.Body.Sections.First.GetRange().Paragraphs.First;
    
    //Access the text in the first run
    Run run = p.GetRange().Runs.First;
    
    //Delete the style applied on the first half
    run.Style.Delete();
    
    //Save the document
    doc.Save("StyleDeleted.docx");
    ```

## Add Linked Styles

Linked styles behave as either a character style or a paragraph style, depending on what you select. So, if you click on a paragraph or select a paragraph and then apply a linked style, the style is applied as a paragraph style. However, if you select a word or phrase in the paragraph and then apply a linked style, the style is applied as a character style, with no effect on the paragraph as a whole. Linked styles also allow a user to update the style properties of all the paragraphs and words or phrases with the same type of style at once.
DsWord supports Linked Styles by using [AddLinkedStyle](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.StyleCollection.AddLinkedStyle.html) method of StyleCollection class, and also provides [HideLinkedCharacterStyles](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.GcWordDocument.HideLinkedCharacterStyles.html) property in GcWordDocument class to hide the character style part of the linked style in our API in order to achieve the same behavior as MS Word.
DsWord also allows a user to:

* Use a linked style in [Add](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.RunCollection.Add.html) or [Insert](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.RunCollection.Insert.html) method of [RunCollection](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.RunCollection.html) for style argument
* Apply a linked style to the [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Run.Style.html) property of [Run](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.Run.html) class
* Apply a linked style to the [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.ContentControl.Style.html) property of [ContentControl](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.ContentControl.html) class
* Apply a linked style to the [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.FormattedMark.Style.html) property of [FormattedMark](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.FormattedMark.html) class
* Apply a linked style to the [Style](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.FindFormatting.Style.html) property of [FindFormatting](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.FindFormatting.html) class.

Refer to the following example code to add paragraph and character linked style:

```csharp
// Initialize GcWordDocument.
GcWordDocument doc = new GcWordDocument();

// Change the default value of HideLinkedCharacterStyles.
doc.HideLinkedCharacterStyles = false;

// Create a linked style.
Style linkedStyle = doc.Styles.AddLinkedStyle("Linked Style", doc.Styles[BuiltInStyleId.Heading1]);

// Change linked style formatting.
linkedStyle.Font.Bold = true;

// Apply linked style to a paragraph.
doc.Body.Paragraphs.Add("This paragraph is formatted with a paragraph linked style.", linkedStyle);

// Apply linked style to a run.
Paragraph paragraph = doc.Body.Paragraphs.Add("This paragraph ", doc.Styles[BuiltInStyleId.Normal]);
Run run = paragraph.GetRange().Runs.Add("is formatted with a character linked style.", linkedStyle);

// Change HideLinkedCharacterStyles property value.
doc.HideLinkedCharacterStyles = true;

// Save the Word document.
doc.Save("LinkedStyles.docx", DocumentType.Document);
```

The output of the above-mentioned example code is shown in the image below:
![](https://cdn.mescius.io/document-site-files/images/d623c730-bceb-4e85-a6b2-a3dbe84720b2/images/linked-styles.png)
Refer to the following example code to add linked style for [ContentControl](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.ContentControl.html), [FormattedMark](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.FormattedMark.html), and [FindFormatting](/document-solutions/dot-net-word-api/api/online/DS.Documents.Word/GrapeCity.Documents.Word.FindFormatting.html):

```csharp
// Initialize GcWordDocument.
GcWordDocument doc = new GcWordDocument();

// Create a linked style 
Style style = doc.Styles.AddLinkedStyle("linked");

// Add a content control.
ContentControl cc = doc.Body.ContentControls.Add(ContentControlType.RichText, false);

// Set the linked style to content control.
cc.Style = style;

// Add a paragraph.
Paragraph p1 = cc.Content.GetRange().Paragraphs.Add("paragraph text");

// Set the linked style to its mark.
p1.Mark.Style = style;

// Add a run and set the linked style to it.
Run run = p1.GetRange().Runs.Add(style);
Text text = run.GetRange().Texts.Add("text to find");

// Add another paragraph and set the linked style to it.
Paragraph p2 = cc.Content.GetRange().Paragraphs.Add("paragraph to find", style);

// Create find options and set the linked style to it.
FindOptions fo = new FindOptions(doc);
fo.FormattingOptions.Style = style;
var res = doc.Body.Find(string.Empty, fo).ToArray();
GrapeCity.Documents.Word.Range found = res[0].Range;

// Save the Word document.
doc.Save("LinkedStyles.docx");
```

For more information on how to apply different document styles using DsWord, see [DsWord sample browser](https://developer.mescius.com/documents-api-word/demos/basics/styles/list-built-in-styles/code-cs).