Set Paragrah Indentation in Word Document – C#/VB.NET

Word indentation is used to adjust distance between text and page margin. It includes four types, first line, hanging, left and right. First line indentation can indent first line of whole paragraph, while hanging indents whole paragraph except first line. Left and right indentations are used to indent whole paragraph toward right or left.

Sometimes, we may set indentation for special paragraphs. For example, the paragraph shows something which we need pay attention or reference contents from books or article websites. Because of indentation, the paragraph becomes easily noticed by readers. Also, when we want to print one document, indentation will be set.

In this post, I want to show how to set indentation in Word by using C#, including first line, left and right indentation.

Note: this method is based on Spire.Doc. If you want to use the following code, please add its dll file as reference.

Detailed Step Shown as Following:

  1. Create a Word document and add section.
  2. Set style for paragraph, including font type, size and color.
  3. Add a new paragraph and append text as title. Set built-in style Title for this paragraph.
  4. Set first line indentation. Add a new paragraph. Append text about indentation style. Then assign value for paragraph.Foramt.FirstLineIndent. Finally, apply style which has been set in step 2.
  5. Set Left indentation. Also, add paragraph and append cue words. Assign value for paragraph.Format.LeftIndent. Apply style again.
  6. Set right indentation. Add paragraph and append cue words. Set alignment as right and assign value for paragraph.Format.RightIndent. Apply style.
  7. Save and launch file.

Main Coding:


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

namespace Indentation
    class indentation
        static void Main(string[] args)

            //Create Document
            Document document = new Document();

            //Add Section
            Section section = document.AddSection();

            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = “Style”;
            style.CharacterFormat.FontName = “Calibri”;
            style.CharacterFormat.FontSize = 14;
            style.CharacterFormat.TextColor = Color.DarkOrange;

            //Add Paragraph and Append Text
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText(“Indent List”);

            //First Line Indent
            paragraph = section.AddParagraph();
            paragraph.AppendText(“First Line Indent”);
            paragraph.Format.FirstLineIndent = 40;

            //Left Indent
            paragraph = section.AddParagraph();
            paragraph.AppendText(“Left Indent”);
            paragraph.Format.LeftIndent = 30;

            //Right Indent
            paragraph = section.AddParagraph();
            paragraph.AppendText(“Right Indent”);
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
            paragraph.Format.RightIndent = 40;


            //Save and Launch
            document.SaveToFile(“Sample.doc”, FileFormat.Doc);


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

Namespace Indentation
    Friend Class indentation
        Shared Sub Main(ByVal args() As String)

            ‘Create Document
            Dim document As New Document()

            ‘Add Section
            Dim section As Section = document.AddSection()

            Dim style As New ParagraphStyle(document)
            style.Name = “Style”
            style.CharacterFormat.FontName = “Calibri”
            style.CharacterFormat.FontSize = 14
            style.CharacterFormat.TextColor = Color.DarkOrange

            ‘Add Paragraph and Append Text
            Dim paragraph As Paragraph = section.AddParagraph()
            paragraph.AppendText(“Indent List”)

            ‘First Line Indent
            paragraph = section.AddParagraph()
            paragraph.AppendText(“First Line Indent”)
            paragraph.Format.FirstLineIndent = 40

            ‘Left Indent
            paragraph = section.AddParagraph()
            paragraph.AppendText(“Left Indent”)
            paragraph.Format.LeftIndent = 30

            ‘Right Indent
            paragraph = section.AddParagraph()
            paragraph.AppendText(“Right Indent”)
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right
            paragraph.Format.RightIndent = 40

            ‘Save and Launch
            document.SaveToFile(“Sample.doc”, FileFormat.Doc)
        End Sub
    End Class
End Namespace

Result Shown as Following:


Click Here to Learn more about Spire.Doc

Click Here to Download Spire.Doc


Leave a Reply

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

You are commenting using your 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