Use DocumentBuilder to inserting a Paragraph within C# and .NET

In modern software development, there is often a need to work with text documents. The SautinSoft.Document library provides convenient tools for manipulating documents in DOCX, RTF, PDF, and other formats. One of the key features is the use of the DocumentBuilder class, which allows for the creation and formatting of text documents programmatically.

In this article, we will focus on inserting a paragraph. To start working with DocumentBuilder, you need to add a reference to the SautinSoft.Document library to your project. After that, you can proceed to coding:

In the code example below, we create a new document, initialize DocumentBuilder, and insert a paragraph with text. The Writeln method automatically adds a newline after the inserted text, allowing you to start a new paragraph. DocumentBuilder also allows you to customize the style of text and paragraphs, add lists, images, and other elements, making it a powerful tool for programmatic document generation. In addition to inserting text, DocumentBuilder can be used to create complex documents using various formatting elements. This can be useful for automating reports, generating documents based on templates, and other tasks where dynamic text document creation is required.

  1. Add SautinSoft.Document from Nuget.
  2. Create a new document.
  3. Create a Builder based on the document.
  4. Use the Writeln method to a add paragraph.

Below is a code example shows how to insert paragraphs (DocumentBuilder.Writeln method) into a document with formatting.

inserting paragraph

Complete code

using SautinSoft.Document;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free trial key here:   
            // https://sautinsoft.com/start-for-free/
            InsertingParagraph();
        }
        /// <summary>
        /// Create a document and insert a paragraph using DocumentBuilder.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-paragraph.php
        /// </remarks>

        static void InsertingParagraph()
        {
            // Create a new document.
            DocumentCore dc = new DocumentCore();
            // Initialize DocumentBuilder with the created document.
            DocumentBuilder db = new DocumentBuilder(dc);

            // Insert a new paragraph.
            db.Writeln("This is an example of a paragraph inserted using DocumentBuilder.");
            db.CharacterFormat.FontName = "Verdana";
            db.CharacterFormat.Size = 16.5f;
            db.CharacterFormat.AllCaps = true;
            db.CharacterFormat.Italic = true;
            db.CharacterFormat.FontColor = Color.Orange;
            db.ParagraphFormat.LeftIndentation = 30;
            db.Writeln("This paragraph has a Left Indentation of 30 points.");
            db.ParagraphFormat.SpecialIndentation = 50;
            db.Writeln("This paragraph retains the Left Indentation of 30 points and is supplemented by the first-line indent of 50 points.");

            // Save the document.
            dc.Save("Example.docx");

            // Important for Linux: Install MS Fonts
            // sudo apt install ttf-mscorefonts-installer -y

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

Download

Imports System
Imports SautinSoft.Document
Imports System.Text


Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			InsertingParagraph()
		End Sub
                ''' Get your free trial key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Create a document and insert a paragraph using DocumentBuilder.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-paragraph.php
		''' </remarks>

		Private Shared Sub InsertingParagraph()
			Dim dc As New DocumentCore()
			Dim db As New DocumentBuilder(dc)

			Dim resultPath As String = "result.docx"

			' Insert the formatted text into the document using DocumentBuilder.
			db.CharacterFormat.FontName = "Verdana"
			db.CharacterFormat.Size = 16.5F
			db.CharacterFormat.AllCaps = True
			db.CharacterFormat.Italic = True
			db.CharacterFormat.FontColor = Color.Orange
			db.ParagraphFormat.LeftIndentation = 30
			db.Writeln("This paragraph has a Left Indentation of 30 points.")
			db.ParagraphFormat.SpecialIndentation = 50
			db.Writeln("This paragraph retains the Left Indentation of 30 points and is supplemented by the first-line indent of 50 points.")

			' This method will clear all directly set formatting values.
			db.ParagraphFormat.ClearFormatting()
			db.CharacterFormat.ClearFormatting()
			db.Write("All directly set text and paragraph formatting values were cleared using DocumentBuilder.")

			' Save the document to the file in DOCX format.
			dc.Save(resultPath)

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) 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:


Captcha

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.