Set Paragraph Indentation for Word Document using Java

Setting paragraph indentation is one of the most often used features in Word documents. In addition to the common indentation form of indenting the first line of each paragraph, you can also indent the whole paragraph to the right or left. The following code snippets will demonstrate how to indent paragraphs for Word document in Java applications.


● Free Spire.Doc for Java
● IntelliJ IDEA


Method 1: Download the Free Spire.Doc for Java and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.


Code Snippet

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

public class WordparaIndent {
public static void main(String[] args) {

//load the sample document
Document document= new Document();

Section section = document.getSections().get(0);

//get the second paragraph and set the left indent
Paragraph para1 = section.getParagraphs().get(1);

//get the third paragraph and set the right indent
Paragraph para2 = section.getParagraphs().get(2);

//get the fourth paragraph and set the first line indent
Paragraph para3 = section.getParagraphs().get(3);

//save the document to file
document.saveToFile("WordIndent.docx", FileFormat.Docx_2013);


Sharing Java Code