Click or drag to resize

CharacterStyle Class

Represents a character style.
Inheritance Hierarchy
SystemObject
  SautinSoft.DocumentStyle
    SautinSoft.DocumentCharacterStyle

Namespace: SautinSoft.Document
Assembly: SautinSoft.Document (in SautinSoft.Document.dll) Version: 2024.11.20
Syntax
public sealed class CharacterStyle : Style

The CharacterStyle type exposes the following members.

Constructors
 NameDescription
Public methodCode exampleCharacterStyle Initializes a new instance of the CharacterStyle class.
Top
Properties
 NameDescription
Public propertyBaseStyle Gets or sets the style on which this style is based.
Public propertyCode exampleCharacterFormat Gets or sets the character format.
Public propertyStyleType Gets the type of the style.
(Overrides StyleStyleType)
Top
Methods
 NameDescription
Public methodEquals Determines whether the specified object is equal to this CharacterStyle instance.
(Overrides ObjectEquals(Object))
Public methodGetHashCode Serves as the default hash function.
(Overrides ObjectGetHashCode)
Top
Remarks
Styles provide a way to format your document in a consistent way so when you change your formatting options on a style, all document elements referencing that style will be changed.
Example

See Developer Guide: This sample shows how to work with styles

This sample shows how to work with styles in C#
using SautinSoft.Document;

namespace Sample
{
    class Sample
    {

        static void Main(string[] args)
        {
            // Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/

            Styles();
        }

        /// <summary>
        /// This sample shows how to work with styles. 
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/styles.php
        /// </remarks>
        public static void Styles()
        {
            string docxPath = @"Styles.docx";

            // Let's create document.
            DocumentCore dc = new DocumentCore();

            // Create custom styles.
            CharacterStyle characterStyle = new CharacterStyle("CharacterStyle1");
            characterStyle.CharacterFormat.FontName = "Arial";
            characterStyle.CharacterFormat.UnderlineStyle = UnderlineType.Wave;
            characterStyle.CharacterFormat.Size = 18;

            ParagraphStyle paragraphStyle = new ParagraphStyle("ParagraphStyle1");
            paragraphStyle.CharacterFormat.FontName = "Times New Roman";
            paragraphStyle.CharacterFormat.Size = 14;
            paragraphStyle.ParagraphFormat.Alignment = HorizontalAlignment.Center;

            // Add styles to the document, then use it.
            dc.Styles.Add(characterStyle);
            dc.Styles.Add(paragraphStyle);

            // Add text content.
            dc.Sections.Add(
            new Section(dc,
                new Paragraph(dc,
                new Run(dc, "Once upon a time, in a far away swamp, there lived an ogre named "),
                new Run(dc, "Shrek") { CharacterFormat = { Style = characterStyle } },
                new Run(dc, " whose precious solitude is suddenly shattered by an invasion of annoying fairy tale characters..."))
                { ParagraphFormat = { Style = paragraphStyle } }));

            // Save our document into DOCX format.
            dc.Save(docxPath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docxPath) { UseShellExecute = true });
        }
    }
}
This sample shows how to work with styles in VB.Net
Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        Styles()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' This sample shows how to work with styles. 
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/styles.php
    ''' </remarks>
    Sub Styles()
        Dim docxPath As String = "Styles.docx"

        ' Let's create document.
        Dim dc As New DocumentCore()

        ' Create custom styles.
        Dim characterStyle As New CharacterStyle("CharacterStyle1")
        characterStyle.CharacterFormat.FontName = "Arial"
        characterStyle.CharacterFormat.UnderlineStyle = UnderlineType.Wave
        characterStyle.CharacterFormat.Size = 18

        Dim paragraphStyle As New ParagraphStyle("ParagraphStyle1")
        paragraphStyle.CharacterFormat.FontName = "Times New Roman"
        paragraphStyle.CharacterFormat.Size = 14
        paragraphStyle.ParagraphFormat.Alignment = HorizontalAlignment.Center

        ' Add styles to the document, then use it.
        dc.Styles.Add(characterStyle)
        dc.Styles.Add(paragraphStyle)


        ' Add text content.
        Dim par As New Paragraph(dc)
        par.ParagraphFormat.Style = paragraphStyle
        dc.Sections.Add(New Section(dc, par))
        par.Inlines.Add(New Run(dc, "Once upon a time, in a far away swamp, there lived an ogre named "))

        Dim run As New Run(dc, "Shrek")
        run.CharacterFormat.Style = characterStyle
        par.Inlines.Add(run)

        par.Inlines.Add(New Run(dc, " whose precious solitude is suddenly shattered by an invasion of annoying fairy tale characters..."))

        ' Save our document into DOCX format.
        dc.Save(docxPath)

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docxPath) With {.UseShellExecute = True})
    End Sub
End Module
See Also