Text

  1. Add SautinSoft.Document from Nuget.
  2. Create a new document.
  3. Add a paragraph.
  4. Add text elements.

In SautinSoft.Document library any text is presented by collection of Inline-derived objects: Run , SpecialCharacter , Hyperlink, Field etc.

The most popular text element is Run.

Run (Inline derived) is a container for one or more text characters having the same character formatting.

To understand how text is organized, let's see a simple Paragraph :

Rich Formatted text

Actually, our Paragraph contains 4 Inline-derived elements:

  • 1 - Run, green text: "This is a rich".
  • 2 - Run, blue text: " formatted text".
  • 3 - SpecialCharacter, type: LineBreak.
  • 4 - Run, black text: "with a line break.".

All these elements are Inline-derived and located inside InlineCollection of the Paragraph.

See the Document Tree Structure to understand hierarchy and relationship between elements.

To illustrate the working with text, let's create same document as in the picture:

Main steps

  1. Notice you are importing the SautinSoft.Document namespace.
    using SautinSoft.Document;
  2. First of all, let's create an instance of DocumentCore with the name dc.
    
    DocumentCore dc = new DocumentCore();
    
    DocumentCore is root class, it represents a document itself.
  3. Add a new Section into our document.
    
        Section section = new Section(dc);
        dc.Sections.Add(section);
    
    Section is a collection of Block elements that are grouped by a unify page properties concept and having specific headers and footers.
  4. Create a new Paragraph element and add our text using the Run and SpecialCharacter:
                // Create a new paragraph and add into the section.
                Paragraph par = new Paragraph(dc);
                section.Blocks.Add(par);
    
                // Create Inline-derived objects with text.
                Run run1 = new Run(dc, "This is a rich");
                run1.CharacterFormat = new CharacterFormat() { FontName = "Times New Roman", Size = 18.0, FontColor = new Color(112, 173, 71) };
    
                Run run2 = new Run(dc, " formatted text");
                run2.CharacterFormat = new CharacterFormat(){ FontName = "Arial", Size = 10.0, FontColor = new Color("#0070C0") };
    
                SpecialCharacter spch3 = new SpecialCharacter(dc, SpecialCharacterType.LineBreak);
    
                Run run4 = new Run(dc, "with a line break.");
                run4.CharacterFormat = new CharacterFormat() { FontName = "Times New Roman", Size = 10.0, FontColor = Color.Black };
    
                // Add our inlines into the paragraph.
                par.Inlines.Add(run1);
                par.Inlines.Add(run2);
                par.Inlines.Add(spch3);
                par.Inlines.Add(run4);
        

We also offer you another way to insert a text inside a document using ContentRange object

Complete code

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/

            AddText();
        }

        /// <summary>
        /// How to create a simple document with text. 
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/text.php
        /// </remarks>
        public static void AddText()
        {
            string documentPath = @"Text.docx";

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

            // Create a new section and add into the document.
            Section section = new Section(dc);
            dc.Sections.Add(section);

            // Create a new paragraph and add into the section.
            Paragraph par = new Paragraph(dc);
            section.Blocks.Add(par);

            // Create Inline-derived objects with text.
            Run run1 = new Run(dc, "This is a rich");
            run1.CharacterFormat = new CharacterFormat() { FontName = "Times New Roman", Size = 18.0, FontColor = new Color(112, 173, 71) };

            Run run2 = new Run(dc, " formatted text");
            run2.CharacterFormat = new CharacterFormat() { FontName = "Arial", Size = 10.0, FontColor = new Color("#0070C0") };

            SpecialCharacter spch3 = new SpecialCharacter(dc, SpecialCharacterType.LineBreak);

            Run run4 = new Run(dc, "with a line break.");
            run4.CharacterFormat = new CharacterFormat() { FontName = "Times New Roman", Size = 10.0, FontColor = Color.Black };

            // Add our inlines into the paragraph.
            par.Inlines.Add(run1);
            par.Inlines.Add(run2);
            par.Inlines.Add(spch3);
            par.Inlines.Add(run4);

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

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        AddText()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' How to create a simple document with text. 
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/text.php
    ''' </remarks>
    Sub AddText()
        Dim documentPath As String = "Text.docx"

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

        ' Create a new section and add into the document.
        Dim section As New Section(dc)
        dc.Sections.Add(section)

        ' Create a new paragraph and add into the section.
        Dim par As New Paragraph(dc)
        section.Blocks.Add(par)

        ' Create Inline-derived objects with text.
        Dim run1 As New Run(dc, "This is a rich")
        run1.CharacterFormat = New CharacterFormat() With {
            .FontName = "Times New Roman",
            .Size = 18.0,
            .FontColor = New Color(112, 173, 71)
        }

        Dim run2 As New Run(dc, " formatted text")
        run2.CharacterFormat = New CharacterFormat() With {
            .FontName = "Arial",
            .Size = 10.0,
            .FontColor = New Color("#0070C0")
        }

        Dim spch3 As New SpecialCharacter(dc, SpecialCharacterType.LineBreak)

        Dim run4 As New Run(dc, "with a line break.")
        run4.CharacterFormat = New CharacterFormat() With {
            .FontName = "Times New Roman",
            .Size = 10.0,
            .FontColor = Color.Black
        }

        ' Add our inlines into the paragraph.
        par.Inlines.Add(run1)
        par.Inlines.Add(run2)
        par.Inlines.Add(spch3)
        par.Inlines.Add(run4)

        ' Save our document into the DOCX format.
        dc.Save(documentPath)

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

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.