Spire.Doc for .NET (337)
Children categories
Inserting paragraphs in Word is a fundamental skill for creating well-structured and organized documents. Paragraphs can break down large blocks of text, making it easier for readers to follow the flow of ideas and find specific information. In Word, you can add new paragraphs to represent new ideas or add additional information. This article will demonstrate how to insert a new paragraph in Word in C# using Spire.Doc for .NET.
- Add a Paragraph at the End of a Word Document in C#
- Insert a Paragraph at a Specified Location in Word in C#
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
Add a Paragraph at the End of a Word Document in C#
To add a new paragraph at the end, you need to get the last section of the Word document through the Document.LastSection property, and then add a paragraph at the end of the section through the Section.AddParagraph() method. The following are the detailed steps:
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get the last section of the document using Document.LastSection property.
- Add a paragraph at the end of the section using Section.AddParagraph() method, and then add text to it using Paragraph.AppendText() method.
- Create a ParagraphStyle object and set the font name, size, style of the paragraph text.
- Apply the paragraph style using Paragraph.ApplyStyle() method
- Save the result document using Document.SaveToFile() method.
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace AddParagraph
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Test.docx");
//Get the last section
Section section = doc.LastSection;
//Add a paragraph at the end and set its text content
Paragraph para = section.AddParagraph();
para.AppendText("Add a paragraph to the end of the document.");
//Set the paragraph style
ParagraphStyle style = new ParagraphStyle(doc);
style.Name = "Style1";
style.CharacterFormat.FontName = "Times New Roman";
style.CharacterFormat.FontSize = 12;
style.CharacterFormat.TextColor = Color.Blue;
style.CharacterFormat.Bold = true;
doc.Styles.Add(style);
para.ApplyStyle("Style1");
para.Format.BeforeSpacing = 10;
//Save the result file
doc.SaveToFile("AddParagraph.docx", FileFormat.Docx2016);
}
}
}

Insert a Paragraph at a Specified Location in Word in C#
You can also add a paragraph and then insert it to a specified position through the Section.Paragraphs.Insert(int index, IParagraph paragraph) method. The following are the detailed steps:
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get a specified section using Document.Sections[] property.
- Add a paragraph using Section.AddParagraph() method, and then add text to it using Paragraph.AppendText() method.
- Set the font name, size, style of the paragraph text.
- Insert the newly added paragraph at a specified index using Section.Paragraphs.Insert(int index, IParagraph paragraph) method.
- Save the result document using Document.SaveToFile() method.
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertParagraph
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Test.docx");
//Get the first section
Section section = doc.Sections[0];
//Add a paragraph and set its text content
Paragraph para = section.AddParagraph();
TextRange textRange = para.AppendText("Insert a paragraph at a specified location in the Word document.");
//Set the font name, size, color and style
textRange.CharacterFormat.TextColor = Color.Blue;
textRange.CharacterFormat.FontName = "Times New Roman";
textRange.CharacterFormat.FontSize = 14;
textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
//Insert the paragraph as the third paragraph
section.Paragraphs.Insert(2, para);
//Set spacing after the paragraph
para.Format.AfterSpacing = 10;
//Save the result file
doc.SaveToFile("InsertParagraph.docx", FileFormat.Docx2016);
}
}
}

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.
In MS Word page border options, there are checkboxes to choose whether page border surrounds header/footer or not. This feature is very important for which can be used to manage the positional relation between page border and header/footer. Spire.Doc supports to set the two frequently-used features: page border and header/footer, and it supports to manage their spatial relations, too. This article is going to introduce the method to set whether page border surrounds header/footer or not.
Note: before start, please download the latest version of Spire.Doc and add the .dll in the bin folder as the reference of Visual Studio.
Step 1: Create a Word document and add a section.
Document document = new Document();
Section section = document.AddSection();
Step 2: Add a sample page border to the document using Spire.Doc library.
section.PageSetup.Borders.BorderType = BorderStyle.Wave;
section.PageSetup.Borders.Color = Color.Green;
section.PageSetup.Borders.Left.Space = 20;
section.PageSetup.Borders.Right.Space = 20;
Step 3: Add sample header and footer to the document using Spire.Doc library.
//add a header and set its format
Paragraph paragraph1 = section.HeadersFooters.Header.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;
TextRange headerText = paragraph1.AppendText("Header isn't included in page border");
headerText.CharacterFormat.FontName = "Calibri";
headerText.CharacterFormat.FontSize = 20;
headerText.CharacterFormat.Bold = true;
//add a footer and set its format
Paragraph paragraph2 = section.HeadersFooters.Footer.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
TextRange footerText = paragraph2.AppendText("Footer is included in page border");
footerText.CharacterFormat.FontName = "Calibri";
footerText.CharacterFormat.FontSize = 20;
footerText.CharacterFormat.Bold = true;
Step 4: Set the header not included in the page border while the footer included.
section.PageSetup.PageBorderIncludeHeader = false;
section.PageSetup.HeaderDistance = 40;
section.PageSetup.PageBorderIncludeFooter = true;
section.PageSetup.FooterDistance = 40;
Step 5: Save the document and launch to see effect.
document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
Effects:

Full Codes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace Mirror_Margin
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();
section.PageSetup.Borders.BorderType = BorderStyle.Wave;
section.PageSetup.Borders.Color = Color.Green;
section.PageSetup.Borders.Left.Space = 20;
section.PageSetup.Borders.Right.Space = 20;
Paragraph paragraph1 = section.HeadersFooters.Header.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;
TextRange headerText = paragraph1.AppendText("Header isn't included in page border");
headerText.CharacterFormat.FontName = "Calibri";
headerText.CharacterFormat.FontSize = 20;
headerText.CharacterFormat.Bold = true;
Paragraph paragraph2 = section.HeadersFooters.Footer.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
TextRange footerText = paragraph2.AppendText("Footer is included in page border");
footerText.CharacterFormat.FontName = "Calibri";
footerText.CharacterFormat.FontSize = 20;
footerText.CharacterFormat.Bold = true;
section.PageSetup.PageBorderIncludeHeader = false;
section.PageSetup.HeaderDistance = 40;
section.PageSetup.PageBorderIncludeFooter = true;
section.PageSetup.FooterDistance = 40;
document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}
Add page numbers in different sections in Word document via Spire.Doc
2015-06-25 08:04:08 Written by KoohjiSometimes in one Word document developers need to add page numbers for different sections, for example cover, directory and content are in different sections. This article talks about how to add page numbers for different sections via Spire.Doc.
Here will import a test document within 3 sections inside as followed screenshot.

Here are the detailed steps:
Step 1: Create a new document and load the test word file.
Document document = new Document("test.docx");
Step 2: Create footer for the first section and add page number inside.
HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();
footerParagraph.AppendField("page number", FieldType.FieldPage);
footerParagraph.AppendText(" of ");
footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages);
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
Step 3: Restart page number of next section and set the starting page number to 1.
document.Sections[1].PageSetup.RestartPageNumbering = true; document.Sections[1].PageSetup.PageStartingNumber = 1;
Step 4: Repeat step2 and Step3 for the rest sections, so change the code with for loop.
for (int i = 0; i < 3; i++)
{
HeaderFooter footer = document.Sections[i].HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();
footerParagraph.AppendField("page number", FieldType.FieldPage);
footerParagraph.AppendText(" of ");
footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages);
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
if (i == 2)
break;
else
{
document.Sections[i + 1].PageSetup.RestartPageNumbering = true;
document.Sections[i + 1].PageSetup.PageStartingNumber = 1;
}
}
Step 5: Save and review.
document.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");
Result screenshot:
