Align Text in Word Document in C#, VB.NET

Word Text Alignment can be taken as one kind of text format tools. It allows users to align text with the following styles: Left, Right, Center and Justify. By default, text will be aligned right. Also, users can customize alignment. For example, the title is often aligned center and the body justified. Below demonstrate the effect by a screenshot.

Word Paragraph Alignment

Spire.Doc for .NET, a professional .NET word component to allow users to manipulate Word documents without automation. This guide will introduce the method to align text with C#, VB.NET via Spire.Doc for .NET. At first, there is a document with default alignment (Left) prepared. Then, four alignments will be applied for four paragraphs. Finally, set HorizontalAlignment property of Format under Paragraph class to realize alignment setting. Download and install Spire.Doc for .NET and use the following code

[C#]
using Spire.Doc;
using Spire.Doc.Documents;

namespace AlignText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document doc = new Document();
            doc.LoadFromFile(@"E:\Work\Documents\WordDocuments\Humor Them.docx");

            //Set Center Alignment
            Section s = doc.Sections[0];
            Paragraph p = s.Paragraphs[0];
            p.Format.HorizontalAlignment = HorizontalAlignment.Center;

            //Set Left Alignment
            Paragraph p1 = s.Paragraphs[1];
            p1.Format.HorizontalAlignment = HorizontalAlignment.Left;

            //Set Right Alignment
            Paragraph p2 = s.Paragraphs[2];
            p2.Format.HorizontalAlignment = HorizontalAlignment.Right;

            //Set Justify Alignment
            Paragraph p3 = s.Paragraphs[3];
            p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;

            //Save and Launch
            doc.SaveToFile("WordAlignment.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("WordAlignment.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace AlignText
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim doc As New Document()
            doc.LoadFromFile("E:\Work\Documents\WordDocuments\Humor Them.docx")

            'Set Center Alignment
            Dim s As Section = doc.Sections(0)
            Dim p As Paragraph = s.Paragraphs(0)
            p.Format.HorizontalAlignment = HorizontalAlignment.Center

            'Set Left Alignment
            Dim p1 As Paragraph = s.Paragraphs(1)
            p1.Format.HorizontalAlignment = HorizontalAlignment.Left

            'Set Right Alignment
            Dim p2 As Paragraph = s.Paragraphs(2)
            p2.Format.HorizontalAlignment = HorizontalAlignment.Right

            'Set Justify Alignment
            Dim p3 As Paragraph = s.Paragraphs(3)
            p3.Format.HorizontalAlignment = HorizontalAlignment.Justify

            'Save and Launch
            doc.SaveToFile("WordAlignment.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("WordAlignment.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, an easy-to-use Word component, enables users to fast generate, open, save and modify Word document on .NET, WPF and Silverlight applications without Word automation and any other third party add-ins.