Documents for Word .NET Edition
Features / Document / Document Styles
In This Topic
    Document 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.

    GcWord 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, GcWord 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

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