C#  

How to Set Line Spacing and Paragraph Spacing in Word Using C#

Line spacing and paragraph spacing play an important role in document readability and layout. Proper spacing can make text easier to read, while tighter spacing helps fit more content into a single page.

In this article, we’ll look at how to programmatically adjust line spacing and paragraph spacing in a Word document using C#.

Understanding Line and Paragraph Spacing

  • Line spacing refers to the vertical distance between lines within the same paragraph.

  • Paragraph spacing refers to the space before and after a paragraph.

In Microsoft Word, these settings can be adjusted manually, but in many automated document generation scenarios, it is more efficient to control them through code.

Environment Setup (Installation)

Before starting with the code, you need to install the required library in your project environment. The easiest way is to install the package using NuGet Package Manager:

PM> Install-Package Spire.Doc

Steps to Set Spacing in a Word Document

The general process for modifying spacing programmatically is as follows:

  1. Create a new document instance.

  2. Add a section to the document.

  3. Insert a paragraph and add text content.

  4. Adjust spacing before and after the paragraph.

  5. Add another paragraph and configure its line spacing.

  6. Save the document to a file.

Example Code in C#

Below is a simple example demonstrating how to set both paragraph spacing and line spacing:

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

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

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

            //Add a paragraph
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("Spire.Doc for .NET is a professional Word .NET library specifically designed for developers to " +
                "create, read, write, convert, compare and print Word documents on any .NET platform " +
                "(Target .NET Framework, .NET Core, .NET Standard, .NET 5.0, .NET 6.0, Xamarin & Mono Android ) with fast and high quality performance.");

            //Set spacing before the paragraph
            paragraph.Format.BeforeSpacing = 30;

            //Set spacing after the paragraph
            paragraph.Format.AfterSpacing = 30;

            //Add another paragraph
            Paragraph paragraph2 = section.AddParagraph();
            paragraph2.AppendText("Spire.Doc for .NET is a reliable API which enables to perform many Word document processing tasks. " +
                "It supports C#, VB.NET, ASP.NET and ASP.NET MVC. Spire.Doc supports Word 97-2003 /2007/2010/2013/2016/2019 " +
                "and it has the ability to convert them to commonly used file formats like XML, RTF, TXT, XPS, EPUB, HTML and vice versa. ");

            //Set line spacing in the paragraph
            paragraph2.Format.LineSpacing = 25;

            //Save the result document
            document.SaveToFile("SetSpacing.docx", FileFormat.Docx);
        }
    }
}

Conclusion

Controlling spacing in Word documents through code is useful when generating reports, templates, or batch documents. With a few simple formatting properties, you can significantly improve the layout quality of generated files.