Set Alignment for Paragraphs in Word in Java

We can set left, centered, right and justified alignment for paragraphs in Word. This article will show you how to set the alignment for paragraphs in a Word document using Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class ParagraphAlighment {
    public static void main(String[] args){
        //Instantiate a Document object
        Document document = new Document();
        
        //Add a section
        Section section = document.addSection();

        //Add a paragraph and make it left aligned
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("This paragraph is left-aligned");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);

        //Add a paragraph and make it centered
        paragraph = section.addParagraph();
        paragraph.appendText("This paragraph is centered");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //Add a paragraph and make it right alighed
        paragraph = section.addParagraph();
        paragraph.appendText("This paragraph is right-aligned");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

        //Add a paragraph and make it justified
        paragraph = section.addParagraph();
        paragraph.appendText("This paragraph is justified");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Justify);

        //Save the resultant document
        document.saveToFile("ParagraphAlignment.docx", FileFormat.Docx_2013);
    }
}

Output:

Set Alignment for Paragraphs in Word in Java