News Category

Paragraph

Paragraph (16)

In Word documents, indentation is a paragraph format used to adjust the distance between paragraph body and page margin. It includes left indent, right indent, first line indent and hanging indent. Left indent and right indent can be applied to all lines of a paragraph, while first line indent can only be applied to first line of a paragraph. As for the hanging indent, it can be applied to every line of the paragraph except the first one. This article introduces how to programmatically set paragraph indents in a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Set Paragraph Indents in Word

The table below lists some of the core classes and properties that are used to set different paragraph indents in a Word document.

Name Description
ParagraphFormat Class Represents the format of a paragraph.
ParagraphFormat.LeftIndent Property Returns or sets the value that represents the left indent for paragraph.
ParagraphFormat.RightIndent Property Returns or sets the value that represents the right indent for paragraph.
ParagraphFormat.FirstLineIndent Property Gets or sets the value for first line or hanging indent.  Positive value represents first-line indent, and Negative value represents hanging indent.

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified paragraph using Section.Paragraphs[] property.
  • Get the paragraph format using Paragraph.Format property, and then set the paragraph indent using the above listed properties of ParagraphFormat class.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordIndent
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a sample Word document
            doc.LoadFromFile("sample.docx");

            //Get the first paragraph and set left indent
            Paragraph para1 = doc.Sections[0].Paragraphs[0];
            para1.Format.LeftIndent = 30;

            //Get the second paragraph and set right indent
            Paragraph para2 = doc.Sections[0].Paragraphs[1];
            para2.Format.RightIndent = 30;

            //Get the third paragraph and set first line indent
            Paragraph para3 = doc.Sections[0].Paragraphs[2];
            para3.Format.FirstLineIndent = 30;

            //Get the fourth paragraph and set hanging indent
            Paragraph para4 = doc.Sections[0].Paragraphs[3];
            para4.Format.FirstLineIndent = -30;

            //Save the document to file
            doc.SaveToFile("Indent.docx", FileFormat.Docx2010);
        }
    }
}

C#/VB.NET: Set Paragraph Indents in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

C#/VB.NET: Align Text in Word

2022-04-14 06:38:00 Written by Administrator

Text alignment is a paragraph formatting attribute that determines the appearance of the text in a whole paragraph. There are four types of text alignments available in Microsoft Word: left-aligned, center-aligned, right-aligned, and justified. In this article, you will learn how to programmatically set different text alignments for paragraphs in a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Align Text in Word

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified paragraph using Section.Paragraphs[] property.
  • Get the paragraph format using Paragraph.Format property
  • Set text alignment for the specified paragraph using ParagraphFormat.HorizontalAlignment property.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace AlignText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a sample Word document
            doc.LoadFromFile(@"D:\Files\sample.docx");

            //Get the first section
            Section section = doc.Sections[0];

            //Get the first paragraph and make it center-aligned
            Paragraph p = section.Paragraphs[0];
            p.Format.HorizontalAlignment = HorizontalAlignment.Center;

            //Get the second paragraph and make it left-aligned
            Paragraph p1 = section.Paragraphs[1];
            p1.Format.HorizontalAlignment = HorizontalAlignment.Left;

            //Get the third paragraph and make it right-aligned
            Paragraph p2 = section.Paragraphs[2];
            p2.Format.HorizontalAlignment = HorizontalAlignment.Right;

            //Get the fourth paragraph and make it justified
            Paragraph p3 = section.Paragraphs[3];
            p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;

            //Save the document
            doc.SaveToFile("WordAlignment.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Align Text in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Page 2 of 2