How to Indent a Paragraph in a Word Document using C#

My word document’s left margin is not even. I want to make the left margin even without disturbing the paragraphs.

@granite61,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A source Word document where margins or indents of Paragraphs are not even.
  • Your expected DOCX file showing the desired output. You can create this output file manually by using MS Word.

As soon as you get these pieces of information ready, we will then start further investigation into your particular scenario and provide you C# code to indent Paragraphs in a Word document correctly.

left margin problem.zip (22.6 KB)

@granite61,

This post explains as to how to indent a Paragraph in a Word Document using C#. You can make use of indentation related properties of Paragraph Format Class. For example, the following properties can be used to set first line (or hanging indent), left and right indentation of Paragraphs in a Word Document:

C# Code to Indent a Paragraph in a Word Document using C#

namespace Aspose.Words.Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            ApplyLicense();

            Document word = new Document("word.docx");

            // set left indent for all Paragraphs in a Word Document to 0
            foreach (Paragraph para in word.GetChildNodes(NodeType.Paragraph, true))
                para.ParagraphFormat.LeftIndent = 0;

            // set hanging indent of first paragraph
            Paragraph first = word.FirstSection.Body.FirstParagraph;
            first.ParagraphFormat.FirstLineIndent = -18;

            // set right indent of second paragraph
            Paragraph second = word.FirstSection.Body.Paragraphs[1];
            second.ParagraphFormat.RightIndent = 72; // 1 inch equals to 72 points

            // save Word to DOCX after Indenting Paragraphs in a Word Document
            word.Save("indent a Paragraph.docx");
        }

        public static void ApplyLicense()
        {
            License license = new License();
            license.SetLicense("path to.lic");
        }
    }
}