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.