Set Text Alignment-Left, Right, Center and Justify, in Word with C#/VB.NET

Text alignment is one kind of paragraph settings in Word. It includes four types, right, left, center and justify. Generally speaking, left is the default alignment style. However, the most frequently used style is justify alignment. In this post, I will introduce a simple method to set text alignment in Word with C#/VB.NET.

Actually, in order to make the layout will be more wonderful and contents to be clearer, people often set text alignment for their Word document. In a Word document, there will be several alignment styles used. For example, if there is a reference paragraph in document, we may set different alignment style for it from others.

In my example, I will show all the four alignment styles. The following steps show details. And a .NET Word component, Spire.Doc for .NET is used, so I have added its dll file as reference in project. 

Steps:

  1. Create a Word document. Add section and add paragraph in this section.
  2. Add cue words to show others what I will do and append style heading 1 for it.
  3. Set alignment.  Firstly, add one paragraph and add text “Horizontal Alignment”. Then set style as heading 2 for this paragraph. Secondly, use foreach sentence to set four alignment styles for text. A, in order to make text be more obvious, I set one style, including font name, size and color. B, add paragraph and append text. Then, set alignment format. C, apply style for text.
  4. Save and launch file.

Main Coding:

C#

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace TextAlignment
{
    class Align
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();

            //Add Section and Paragraph
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Append Text
            paragraph.AppendText(“Text Alignment Setting”);
            paragraph.ApplyStyle(BuiltinStyle.Heading1);

            //Append Alignment
            AppendAligmentText(section,document);

            //Save and Launch
            document.SaveToFile(“Alignment.docx”,FileFormat.Docx);
            System.Diagnostics.Process.Start(“Alignment.docx”);
        }

        static void AppendAligmentText(Section section, Document document)
        {
            Paragraph paragraph = null;
            paragraph = section.AddParagraph();

            //Append Text
            paragraph.AppendText(“Horizontal Aligenment”);
            paragraph.ApplyStyle(BuiltinStyle.Heading3);
            foreach (Spire.Doc.Documents.HorizontalAlignment align in Enum.GetValues(typeof(Spire.Doc.Documents.HorizontalAlignment)))
            {

                //Set Style
                ParagraphStyle style = new ParagraphStyle(document);
                style.CharacterFormat.FontName = “Calibri”;
                style.CharacterFormat.FontSize = 13;
                style.CharacterFormat.TextColor = Color.DarkCyan;
                document.Styles.Add(style);

                //Set Alignment
                Paragraph paragraph1 = section.AddParagraph();
                paragraph1.AppendText(“This text is “ + align.ToString());
                paragraph1.Format.HorizontalAlignment = align;

                //Apply Style
                paragraph1.ApplyStyle(style.Name);
            }
        }
    }
}

VB.NET

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace TextAlignment
    Friend Class Align
        Shared Sub Main(ByVal args() As String)
            ‘Create Document
            Dim document As New Document()

            ‘Add Section and Paragraph
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = section.AddParagraph()

            ‘Append Text
            paragraph.AppendText(“Text Alignment Setting”)
            paragraph.ApplyStyle(BuiltinStyle.Heading1)

            ‘Append Alignment
            AppendAligmentText(section, document)

            ‘Save and Launch
            document.SaveToFile(“Alignment.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Alignment.docx”)
        End Sub

        Private Shared Sub AppendAligmentText(ByVal section As Section, ByVal document As Document)
            Dim paragraph As Paragraph = Nothing
            paragraph = section.AddParagraph()

            ‘Append Text
            paragraph.AppendText(“Horizontal Aligenment”)
            paragraph.ApplyStyle(BuiltinStyle.Heading3)
            For Each align As Spire.Doc.Documents.HorizontalAlignment In System.Enum.GetValues(GetType(Spire.Doc.Documents.HorizontalAlignment))

                ‘Set Style
                Dim style As New ParagraphStyle(document)
                style.CharacterFormat.FontName = “Calibri”
                style.CharacterFormat.FontSize = 13
                style.CharacterFormat.TextColor = Color.DarkCyan
                document.Styles.Add(style)

                ‘Set Alignment
                Dim paragraph1 As Paragraph = section.AddParagraph()
                paragraph1.AppendText(“This text is “ & align.ToString())
                paragraph1.Format.HorizontalAlignment = align

                ‘Apply Style
                paragraph1.ApplyStyle(style.Name)
            Next align
        End Sub
    End Class
End Namespace

Result Shown by Following:

Download Spire.Doc for .NET Here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s