In Word documents, indentation is a paragraph format used to adjust the distance between paragraph body and page margin. It includes left indent, right indent, first line indent and hanging indent. Left indent and right indent can be applied to all lines of a paragraph, while first line indent can only be applied to first line of a paragraph. As for the hanging indent, it can be applied to every line of the paragraph except the first one. This article introduces how to programmatically set paragraph indents in a Word document 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 Paragraph Indents in Word

The table below lists some of the core classes and properties that are used to set different paragraph indents in a Word document.

Name Description
ParagraphFormat Class Represents the format of a paragraph.
ParagraphFormat.LeftIndent Property Returns or sets the value that represents the left indent for paragraph.
ParagraphFormat.RightIndent Property Returns or sets the value that represents the right indent for paragraph.
ParagraphFormat.FirstLineIndent Property Gets or sets the value for first line or hanging indent.  Positive value represents first-line indent, and Negative value represents hanging indent.

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified paragraph using Section.Paragraphs[] property.
  • Get the paragraph format using Paragraph.Format property, and then set the paragraph indent using the above listed properties of ParagraphFormat class.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

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

            //Load a sample Word document
            doc.LoadFromFile("sample.docx");

            //Get the first paragraph and set left indent
            Paragraph para1 = doc.Sections[0].Paragraphs[0];
            para1.Format.LeftIndent = 30;

            //Get the second paragraph and set right indent
            Paragraph para2 = doc.Sections[0].Paragraphs[1];
            para2.Format.RightIndent = 30;

            //Get the third paragraph and set first line indent
            Paragraph para3 = doc.Sections[0].Paragraphs[2];
            para3.Format.FirstLineIndent = 30;

            //Get the fourth paragraph and set hanging indent
            Paragraph para4 = doc.Sections[0].Paragraphs[3];
            para4.Format.FirstLineIndent = -30;

            //Save the document to file
            doc.SaveToFile("Indent.docx", FileFormat.Docx2010);
        }
    }
}

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

A page break is a marker that controls where one page ends and where a new page begins. If you want to move the content after a certain place to the next page in your Word document, you can insert a page break. In this article, you will learn how to insert page break into Word documents in C# and VB.NET using Spire.Doc for .NET library.

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

Insert Page Break after a Specific Paragraph

The following are the steps to insert page break after a specific paragraph:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the desired section using Document.Sections[sectionIndex] property.
  • Get the desired paragraph using Section.Paragraphs[paragraphIndex] property.
  • Add a page break to the paragraph using Paragraph.AppendBreak(BreakType.PageBreak) method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertPageBreakAfterParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Get the first section
            Section section = document.Sections[0];
            //Get the 2nd paragraph in the section
            Paragraph paragraph = section.Paragraphs[1];

            //Append a page break to the paragraph
            paragraph.AppendBreak(BreakType.PageBreak);

            //Save the result document
            document.SaveToFile("InsertPageBreak.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Insert Page Break into Word Documents

Insert Page Break after a Specific Text

The following are the steps to insert a page break after a specific text:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Find a specific text using Document.FindString() method.
  • Access the text range of the searched text using TextSelection.GetAsOneRange() method.
  • Get the paragraph where the text range is located using ParagraphBase.OwnerParagraph property.
  • Get the position index of the text range in the paragraph using Paragraph.ChildObjects.IndexOf() method.
  • Initialize an instance of Break class to create a page break.
  • Insert the page break after the searched text using Paragraph.ChildObjects.Insert() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

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

            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Search a specific text 
            TextSelection selection = document.FindString("celebration", true, true);
            //Get the text range of the seached text
            TextRange range = selection.GetAsOneRange();
            //Get the paragraph where the text range is located
            Paragraph paragraph = range.OwnerParagraph;
            //Get the position index of the text range in the paragraph
            int index = paragraph.ChildObjects.IndexOf(range);

            //Create a page break
            Break pageBreak = new Break(document, BreakType.PageBreak);
            //Insert the page break after the searched text
            paragraph.ChildObjects.Insert(index + 1, pageBreak);

            //Save the result document
            document.SaveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Insert Page Break into Word Documents

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.

The comment feature in Microsoft Word provides an excellent way for people to add their insights or opinions to a Word document without having to change or interrupt the content of the document. If someone comments on a document, the document author or other users can reply to the comment to have a discussion with him, even if they're not viewing the document at the same time. This article will demonstrate how to add, reply to or delete comments in Word in C# and VB.NET using Spire.Doc for .NET library.

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 Comment to Paragraph in Word in C# and VB.NET

Spire.Doc for .NET provides the Paragraph.AppendComment() method to add a comment to a specific paragraph. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Access a specific section in the document by its index through Document.Sections[int] property.
  • Access a specific paragraph in the section by its index through Section.Paragraphs[int] property.
  • Add a comment to the paragraph using Paragraph.AppendComment() method.
  • Set the author of the comment through Comment.Format.Author property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace AddComments
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"Sample.docx");

            //Get the first section in the document
            Section section = document.Sections[0];

            //Get the first paragraph in the section
            Paragraph paragraph = section.Paragraphs[0];
            //Add a comment to the paragraph
            Comment comment = paragraph.AppendComment("This comment is added using Spire.Doc for .NET.");
            //Set comment author
            comment.Format.Author = "Eiceblue";
            comment.Format.Initial = "CM";

            //Save the result document
            document.SaveToFile("AddCommentToParagraph.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Add, Reply to or Delete Comments in Word

Add a Comment to Text in Word in C# and VB.NET

The Paragraph.AppendComment() method is used to add comments to an entire paragraph. By default, the comment marks will be placed at the end of the paragraph. To add a comment to a specific text, you need to search for the text using Document.FindString() method, then place the comment marks at the beginning and end of the text. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Find the specific text in the document using Document.FindString() method.
  • Create a comment start mark and a comment end mark, which will be placed at the beginning and end of the found text respectively.
  • Initialize an instance of the Comment class to create a new comment. Then set the content and author for the comment.
  • Get the owner paragraph of the found text. Then add the comment to the paragraph as a child object.
  • Insert the comment start mark before the text range and the comment end mark after the text range.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace AddCommentsToText
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"CommentTemplate.docx");

            //Find a specific string
            TextSelection find = document.FindString("Microsoft Office", false, true);

            //Create the comment start mark and comment end mark
            CommentMark commentmarkStart = new CommentMark(document);
            commentmarkStart.Type = CommentMarkType.CommentStart;
            CommentMark commentmarkEnd = new CommentMark(document);
            commentmarkEnd.Type = CommentMarkType.CommentEnd;

            //Create a comment and set its content and author
            Comment comment = new Comment(document);
            comment.Body.AddParagraph().Text = "Developed by Microsoft.";
            comment.Format.Author = "Shaun";

            //Get the found text as a single text range
            TextRange range = find.GetAsOneRange();

            //Get the owner paragraph of the text range
            Paragraph para = range.OwnerParagraph;

            //Add the comment to the paragraph
            para.ChildObjects.Add(comment);

            //Get the index of text range in the paragraph
            int index = para.ChildObjects.IndexOf(range);

            //Set comment ID for the comment mark start and comment mark end
            commentmarkStart.CommentId = comment.Format.CommentId;
            commentmarkEnd.CommentId = comment.Format.CommentId;

            //Insert the comment start mark before the text range
            para.ChildObjects.Insert(index, commentmarkStart);
            //Insert the comment end mark after the text range
            para.ChildObjects.Insert(index + 2, commentmarkEnd);

            //Save the result document
            document.SaveToFile("AddCommentForText.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Add, Reply to or Delete Comments in Word

Reply to a Comment in Word in C# and VB.NET

To add a reply to an existing comment, you can use the Comment.ReplyToComment() method. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific comment in the document through Document.Comments[int] property.
  • Initialize an instance of the Comment class to create a new comment. Then set the content and author for the comment.
  • Add the new comment as a reply to the specific comment using Comment.ReplyToComment() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Fields;

namespace ReplyToComments
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"AddCommentToParagraph.docx");

            //Get the first comment in the document
            Comment comment1 = document.Comments[0];

            //Create a new comment and specify its author and content
            Comment replyComment1 = new Comment(document);
            replyComment1.Format.Author = "Michael";
            replyComment1.Body.AddParagraph().AppendText("Spire.Doc is a wonderful Word library.");

            //Add the comment as a reply to the first comment
            comment1.ReplyToComment(replyComment1);

            //Save the result document
            document.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Add, Reply to or Delete Comments in Word

Delete Comments in Word in C# and VB.NET

Spire.Doc for .NET offers the Document.Comments.RemoveAt(int) method to remove a specific comment from a Word document and the Document.Comments.Clear() method to remove all comments from a Word document. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Delete a specific comment or all comments in the document using Document.Comments.RemoveAt(int) method or Document.Comments.Clear() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace DeleteComments
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile(@"AddCommentToParagraph.docx");

            //Delete the first comment in the document
            document.Comments.RemoveAt(0);

            //Delete all comments in the document
            //document.Comments.Clear();

            //Save the result document
            document.SaveToFile("DeleteComment.docx", FileFormat.Docx2013);
            document.Close();
        }
    }
}

C#/VB.NET: Add, Reply to or Delete Comments 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.

page 318