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:

C#

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();

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

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

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

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

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

            paragraph.ApplyStyle(style.Name);

            //Save and Launch
            document.SaveToFile(“Sample.doc”, FileFormat.Doc);
            System.Diagnostics.Process.Start(“Sample.doc”);
        }
    }
}

VB.NET

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()

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

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

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

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

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

            ‘Save and Launch
            document.SaveToFile(“Sample.doc”, FileFormat.Doc)
            System.Diagnostics.Process.Start(“Sample.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

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

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