C#/VB.NET: Set Line Spacing and Paragraph Spacing in Word

Line spacing is the amount of white space between each line in a paragraph, while paragraph spacing is the amount of white space before and after each paragraph in a document. In MS Word, you can adjust the spacing manually if the default spacing does not meet your needs. In this article, you will learn how to programmatically set line spacing and paragraph spacing in Word 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 Line Spacing and Paragraph Spacing in Word

Loose line or paragraph spacing can make text more readable, while tight line or paragraph spacing can fit more text in a document. Below are the steps to adjust the line spacing and paragraph spacing in a Word document.

  • Create a Document instance.
  • Add a section to the document using Document.AddSection() method, and then add a paragraph to the section.
  • Set the before and after spacing for the paragraph using Paragraph.Format.BeforeSpacing and Paragraph.Format.AfterSpacing properties.
  • Add another paragraph and set line spacing in the paragraph using Paragraph.Format.LineSpacing property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
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);
        }
    }
}

C#/VB.NET: Set Line Spacing and Paragraph Spacing 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.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 31 May 2023 09:04