Wednesday, 19 January 2011 06:38

Set Word Indent in C#, VB.NET

Word Indent, a kind of paragraph format, is used to adjust distance between paragraph body and page margins. It includes left indent, right indent, first line indent and hanging indent. Frist indent and right indent can be applied for all paragraph body, while first line indent just can be used for first line of one paragraph and hanging indent for paragraph body except the first line. This guide introduces a solution to set Word indent in C# and VB.NET via Spire.Doc for .NET. The following screenshot presents result after setting indents.

Word Paragraph Indent

Spire.Doc for .NET, powerful .NET Word component, provides a ParagraphFormat class to enables users to set format for specified paragraph and indents are format properties of ParagraphFormat class. Firstly, get specified paragraphs to set indents in document. Secondly, set LeftIndent, RightIndent, FirstLineIndent properties for these paragraphs. Please note that type of all indent properties is float. Download and install Spire.Doc for .NET and follow the code blow to set Word indents.

using Spire.Doc;
using Spire.Doc.Documents;

namespace WordIndent
    class Program
        static void Main(string[] args)
            Document doc = new Document();
            doc.LoadFromFile(@"E:\Work\Documents\WordDocuments\Microsoft Word 2013 Preview.docx");

            Paragraph para1 = doc.Sections[0].Paragraphs[0];
            para1.Format.LeftIndent = 30;

            Paragraph para2 = doc.Sections[0].Paragraphs[2];
            para2.Format.FirstLineIndent = 30;

            Paragraph para3 = doc.Sections[0].Paragraphs[4];
            para3.Format.RightIndent = 80;

            doc.SaveToFile("Indent.docx", FileFormat.Docx2010);
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordIndent
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            Dim doc As New Document()
            doc.LoadFromFile("E:\Work\Documents\WordDocuments\Microsoft Word 2013 Preview.docx")

            Dim para1 As Paragraph = doc.Sections(0).Paragraphs(0)
            para1.Format.LeftIndent = 30

            Dim para2 As Paragraph = doc.Sections(0).Paragraphs(2)
            para2.Format.FirstLineIndent = 30

            Dim para3 As Paragraph = doc.Sections(0).Paragraphs(4)
            para3.Format.RightIndent = 80

            doc.SaveToFile("Indent.docx", FileFormat.Docx2010)
        End Sub
    End Class
End Namespace

Spire.Doc, specializing in manipulating Word document, enables users to generate, write, modify and save Word document (.doc/.docx) quickly and easily in .NET, Silverlight and WPF with C# and VB.NET.

Published in Paragraph