Java: Set Paragraph Indents in Word

In Microsoft Word, we can set the paragraph indents as left, right, first line or hanging. In this article, we will demonstrate how to achieve this functionality 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.


Set Paragraph Indents in Word

Spire.Doc for Java provides a ParagraphFormat class to work with paragraph formatting. You can use Paragraph.getFormat() method to get the ParagraphFormat object, and then use the following methods to set the corresponding paragraph indents with the object:

Indents Methods Descriptions
Left ParagraphFormat.setLeftIndent(float value) Indents the paragraph on the left by the amount you set.
Right ParagraphFormat.setRightIndent(float value) Indents the paragraph on the right by the amount you set.
First line ParagraphFormat.setFirstLineIndent(float value) Indent the first line of a paragraph.
Hanging ParagraphFormat.setFirstLineIndent(float negativeValue) Sets off the first line of a paragraph by positioning it at the margin, and then indenting each subsequent line of the paragraph.

The following are the steps to set paragraph indents:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Get the desired section by its index using Document.getSections.get() method.
  • Get the desired paragraph by index using Section.getParagraphs.get() method.
  • Get the ParagraphFormat object using Paragraph.getFormat() method.
  • Call the methods listed in above table to set paragraph indents with the object.
  • 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.Paragraph;
import com.spire.doc.formatting.ParagraphFormat;

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

        //Get the first section
        Section section = document.getSections().get(0);

        //Get the second paragraph and set left indent
        Paragraph para = section.getParagraphs().get(1);
        ParagraphFormat format = para.getFormat();

        //Get the third paragraph and set right indent
        para = section.getParagraphs().get(2);
        format = para.getFormat();

        //Get the fourth paragraph and set first line indent
        para = section.getParagraphs().get(3);
        format = para.getFormat();

        //Get the fifth paragraph and set hanging indent
        para = section.getParagraphs().get(4);
        format = para.getFormat();

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

The following is the output Word document after setting paragraph indents:

Java: 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.