Document .Net - Formatting and Styles

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)
        {
            FormattingAndStyles();
        }
        /// <summary>
        /// Creates a new document and applies formatting and styles.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/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 demonstation purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docxPath) { UseShellExecute = true });
        }
    }
}

Download.

        
            Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing

Module Sample
    Sub Main()
        FormattingAndStyles()
    End Sub

    ''' <summary>
    ''' Creates a new document and applies formatting and styles.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/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 demonstation purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docxPath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace

Download.

© SautinSoft 2002 - 2018