How to set the spacing before and after the paragraph in C#

With Spire.Doc, we can set the formats for paragraph in C#. This article will focus on demonstrate how to set the spacing before and after the paragraph in C#.

Set the spacing before and after the paragraph for a newly added paragraph added by the method of paragraph.AppendHTML() to a new blank word document.

//create a new word document and add a section and paragraph to it.
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();

//Add the text strings to the paragraph and set the style
para.AppendHTML("

Add a new paragraph to the word and set the spacing

"); para.ApplyStyle(BuiltinStyle.Heading1); //set the spacing before and after para.Format.BeforeAutoSpacing = false; para.Format.BeforeSpacing = 20; para.Format.AfterAutoSpacing = false; para.Format.AfterSpacing = 20; //save the document to file doc.SaveToFile("Result1.docx");

How to set the spacing before and after the paragraph in C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SetSpacing
{
 class Program
    {

     static void Main(string[] args)
     {
         //create a new word document and load the sample from file
         Document document = new Document();
         document.LoadFromFile("sample.docx", FileFormat.Docx);

         //Add the text strings to the paragraph and set the style
         Paragraph para = new Paragraph(document);
         TextRange textRange1 = para.AppendText("This is a inserted paragraph.");
         textRange1.CharacterFormat.TextColor = Color.Blue;
         textRange1.CharacterFormat.FontSize = 15;

         //set the spacing before and after
         para.Format.BeforeAutoSpacing = false;
         para.Format.BeforeSpacing = 10;
         para.Format.AfterAutoSpacing = false;
         para.Format.AfterSpacing = 10;

         //insert the added paragraph to the word document
         document.Sections[0].Paragraphs.Insert(1, para);

         //save the document to file
         document.SaveToFile("Result2.docx", FileFormat.Docx2010);
     }

    }
}

How to set the spacing before and after the paragraph in C#