Document Solutions for Word
Features / Styles
In This Topic
    Styles
    In This Topic

    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 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 class. You can add a defined style to the style collection using Add method of the StyleCollection class and the style collection can be accessed using Styles property of the GcWordDocument 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 property of the Run class.
      C#
      Copy Code
      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");
    Back to Top

    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.
      C#
      Copy Code
      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");
    Back to Top

    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 method of the Style class.
      C#
      Copy Code
      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");

    Back to Top

    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 method of StyleCollection class, and also provides HideLinkedCharacterStyles 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:

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

    C#
    Copy Code
    // 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:

    Refer to the following example code to add linked style for ContentControlFormattedMark, and FindFormatting:

    C#
    Copy Code
    // 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");

    Back to Top

    For more information on how to apply different document styles using DsWord, see DsWord sample browser.