Formatting and Styles


Styles make a document more compact by reducing the number of repeated definitions and properties, and the amount of work required to make changes to the document's appearance. There are several styles type: CharacterStyle, ParagraphStyle, TableStyle and ListStyle.

For example, using CharacterStyle, the appearance of all the pieces of text that share a common style can be changed in one place, in that style's definition.

Complete code

using SautinSoft.Document;
using SautinSoft.Document.Drawing;

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

            FormattingAndStyles();
        }
        /// <summary>
        /// Creates a new document and applies formatting and styles.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/formatting-and-styles.php
        /// </remarks>
        static void FormattingAndStyles()
        {
            string docxPath = @"FormattingAndStyles.docx";

            // Let's create a new document.
            DocumentCore dc = new DocumentCore();            
            Run run1 = new Run(dc, "This is Run 1 with character format Green. ");
            Run run2 = new Run(dc, "This is Run 2 with style Red.");
            
            // Create a new character style.
            CharacterStyle redStyle = new CharacterStyle("Red");
            redStyle.CharacterFormat.FontColor = Color.Red;
            dc.Styles.Add(redStyle);
            
            // Apply the direct character formatting.            
            run1.CharacterFormat.FontColor = Color.DarkGreen;

            // Apply only the style.
            run2.CharacterFormat.Style = redStyle;
            
            dc.Content.End.Insert(run1.Content);
            dc.Content.End.Insert(run2.Content);

            // 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 });
        }
    }
}

Download

Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing

Namespace Example
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            FormattingAndStyles()
        End Sub
        ''' Get your free 30-day key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' Creates a new document and applies formatting and styles.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/formatting-and-styles.php
        ''' </remarks>
        Private Shared Sub FormattingAndStyles()
            Dim docxPath As String = "FormattingAndStyles.docx"

            ' Let's create a new document.
            Dim dc As New DocumentCore()
            Dim run1 As New Run(dc, "This is Run 1 with character format Green. ")
            Dim run2 As New Run(dc, "This is Run 2 with style Red.")

            ' Create a new character style.
            Dim redStyle As New CharacterStyle("Red")
            redStyle.CharacterFormat.FontColor = Color.Red
            dc.Styles.Add(redStyle)

            ' Apply the direct character formatting.            
            run1.CharacterFormat.FontColor = Color.DarkGreen

            ' Apply only the style.
            run2.CharacterFormat.Style = redStyle

            dc.Content.End.Insert(run1.Content)
            dc.Content.End.Insert(run2.Content)

            ' 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 Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.