Java: Set Paragraph Alignments in Word

In Microsoft Word, we can set the text alignment of a paragraph as left/center/right/justified/distributed. In this article, we will demonstrate how to achieve this function programmatically in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

  • Package Manager
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>4.9.0</version>
    </dependency>
</dependencies>

Set Paragraph Alignments in Word

Spire.Doc for Java provides the ParagraphFormat class to work with paragraph formatting. You can use Paragraph.getFormat() method to get the ParagraphFormat object, and then use ParagraphFormat.setHorizontalAlignment() method to set the text alignment for the paragraph.

The following are the steps to set the text alignment for a paragraph in a Word document:

  • Create a Document instance.
  • Add a section to the document using Document.addSection() method.
  • Add a paragraph to the section using Section.addParagraph() method, then append text to the paragraph using Paragraph.appendText() method.
  • Get the ParagraphFormat object using Paragraph.getFormat() method.
  • Set text alignment for the paragraph using ParagraphFormat.setHorizontalAlignment() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.formatting.ParagraphFormat;

public class ParagraphAlignments {
    public static void main(String[] args) {
        //Create a Document instance
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

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

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

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

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

        //Add a paragraph and make it distributed
        para = section.addParagraph();
        para.appendText("This paragraph is distributed");
        format = para.getFormat();
        format.setHorizontalAlignment(HorizontalAlignment.Distribute);

        //Save the result document
        document.saveToFile("ParagraphAlignments.docx", FileFormat.Docx_2013);
    }
}

Java: Set Paragraph Alignments 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.