Paragraph (8)
Every paragraph in a Word document uses a paragraph style, either intentionally or unintentionally. The paragraph style can be a built-in style, such as Heading 1 and Heading 2, or it can be a customized style. This article introduces how we can extract paragraphs that use a specific style by using Spire.Doc for Java.
The table below lists the style names in MS Word and their corresponding names in Spire.Doc. A very simple rule is that the style name returned by programming does not contain spaces.
| Style Name in MS Word | Style Name in Spire.Doc |
| Title | Title |
| Subtitle | Subtitle |
| Heading 1 | Heading1 |
| Heading 2 | Heading2 |
| Heading 3 | Heading3 |
| No Spacing | NoSpacing |
| Quote | Quote |
| Intense Quote | IntenseQuote |
| List Paragraph | ListParagraph |
| Normal | Normal |
| Custom Name | CustomName |
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.
<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>13.11.2</version>
</dependency>
</dependencies>
Extract Paragraphs that Use a Specific Style
The style name of a specific paragraph can be obtained by Paragraph.getStyleName() method. If a paragraph’s style name is exactly what you want to query, you can get the paragraph content using Paragraph.getText() method. The following are the steps to extract paragraphs that use a specific style.
- Load a sample Word document while initializing the Document object.
- Loop through the sections in the document.
- Get a specific paragraph from a certain section using Section.getParagraphs().get() method.
- Get the paragraph's style name using Paragraph.getStyleName() method and determine if the style is "Heading 1".
- If yes, extract the text of the paragraph using Paragraph.getText() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.documents.Paragraph;
public class GetParagraphByStyleName {
public static void main(String[] args) {
//Load a sample Word document while initializing the Document object
Document doc = new Document("C:\\Users\\Administrator\\Desktop\\Styles.docx");
//Declare a variable
Paragraph paragraph;
//Loop through the sections
for (int i = 0; i < doc.getSections().getCount(); i++) {
//Loop through the paragraphs of a specific section
for (int j = 0; j < doc.getSections().get(i).getParagraphs().getCount(); j++) {
//Get a specific paragraph
paragraph = doc.getSections().get(i).getParagraphs().get(j);
//Determine if the paragraph style is "Heading 1"
if (paragraph.getStyleName().equals("Heading1")) {
//Get the text of the paragraph in "Heading 1"
System.out.println("Heading 1: " + paragraph.getText() + "\n");
}
//Determine if the paragraph style is "My Custom Style"
if (paragraph.getStyleName().equals("MyCustomStyle")) {
//Get the text of the paragraph in "My Custom Style"
System.out.println("My Custom Style: " + paragraph.getText());
}
}
}
}
}

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.
In the process of manipulating Word documents, it is sometimes necessary to keep some important information from others. For this reason, we can hide them to ensure confidentiality. This article shows how to hide a specific paragraph in a Word document 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.
<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>13.11.2</version>
</dependency>
</dependencies>
Hide a Specific Paragraph in Word
Spire.Doc for Java supports hiding a specific paragraph in Word by using TextRange.getCharacterFormat().setHidden(boolean value) method. Here are detailed steps to follow.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Get a specific section of the Word document using Document.getSections().get() method.
- Get a specific paragraph of the section using Section.getParagraphs().get() method.
- Loop through the child objects of the paragraph, and convert each child object as a text range if it is plain text. Then hide the text range using TextRange.getCharacterFormat().setHidden(boolean value) method.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
public class HideParagraph {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load a sample Word document
document.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");
//Get a specific section of Word
Section sec = document.getSections().get(0);
//Get a specific paragraph of the section
Paragraph para = sec.getParagraphs().get(1);
//Loop through the child objects
for (Object docObj : para.getChildObjects()) {
DocumentObject obj = (DocumentObject)docObj;
//Determine if a child object is an instance of TextRange
if ((obj instanceof TextRange)) {
TextRange range = ((TextRange)(obj));
//Hide the text range
range.getCharacterFormat().setHidden(true);
}
}
//Save the document to another file
document.saveToFile("output/hideParagraph.docx", FileFormat.Docx_2013);
}
}

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.
In MS Word, it is quite essential to delete paragraphs that contain duplicate content or information that is not relevant to the subject matter. By doing so, you can simplify your document and ensure the accuracy of the information. In this article, you will learn how to programmatically remove paragraphs in a Word document using Spire.Doc for Java.
- Remove All Paragraphs in a Word Document in Java
- Remove a Specific Paragraph in a Word Document in Java
- Remove Blank Paragraphs in a Word Document in 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.
<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>13.11.2</version>
</dependency>
</dependencies>
Remove All Paragraphs in a Word Document in Java
To remove all paragraphs, you need to loop through all sections in a document and then delete all paragraphs in each section using Section.getParagraphs().clear() method. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Traverse through each section of the document and then remove all paragraphs in the section using Section.getParagraphs().clear() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
public class removeAllParagraphs {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load a sample document from disk
document.loadFromFile("E:\\Files\\test23.docx");
//Remove paragraphs from every section in the document
for ( Object sectionObj: document.getSections()) {
Section section = (Section)sectionObj;
section.getParagraphs().clear();
}
//Save the result document
document.saveToFile("removeAllParagraphs.docx", FileFormat.Docx_2013);
}
}

Remove a Specific Paragraph in a Word Document in Java
If you find a paragraph that contains duplicate or useless information, Spire.Doc for Java allows you to delete the specified paragraph using Section.getParagraphs().removeAt() method. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Get a specified section of the document using Document.getSections().get() method.
- Remove a specified paragraph in the section using Section.getParagraphs().removeAt() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
public class removeSpecificParagraph {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load a sample document from disk
document.loadFromFile("E:\\Files\\test23.docx");
//Get the first section of the document
Section section = document.getSections().get(0);
//Remove the third paragraph in the section
section.getParagraphs().removeAt(2);
//Save the result document
document.saveToFile("removeSpecificParagraph.docx", FileFormat.Docx_2013);
}
}

Remove Blank Paragraphs in a Word Document in Java
When there are many empty paragraphs/lines in a document, it's necessary to remove them to improve readability. The following are the steps to remove all blank paragraphs/lines in a Word document.
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Traverse through all paragraphs in the document and determine whether the paragraph is a blank paragraph.
- Remove blank paragraphs from the document using Section.getBody().getChildObjects().remove() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class removeEmptyLines {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load a sample document from disk
document.loadFromFile("E:\\Files\\test230.docx");
//Traverse each paragraph in the Word document
for (Object sectionObj : document.getSections()) {
Section section=(Section)sectionObj;
for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
if ((section.getBody().getChildObjects().get(i).getDocumentObjectType().equals(DocumentObjectType.Paragraph) )) {
String s= ((Paragraph)(section.getBody().getChildObjects().get(i))).getText().trim();
//Determine if the paragraph is a blank paragraph
if (s.isEmpty()) {
//Remove blank paragraphs
section.getBody().getChildObjects().remove(section.getBody().getChildObjects().get(i));
i--;
}
}
}
}
//Save the result document
document.saveToFile("removeEmptyLines.docx", FileFormat.Docx_2013);
}
}

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.
This article shows you how to set ASCII characters (special symbols) as bullet points in Word documents using Spire.Doc for Java.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.ListStyle;
import com.spire.doc.documents.ListType;
import com.spire.doc.documents.Paragraph;
public class SetBulletPoints {
public static void main(String[] args) {
//Create a Document object and add a section
Document doc = new Document();
Section section = doc.addSection();
//Create four list styles based on different ASCII characters
ListStyle listStyle1 = new ListStyle(doc, ListType.Bulleted);
listStyle1.getLevels().get(0).setBulletCharacter("\u006e");
listStyle1.getLevels().get(0).getCharacterFormat().setFontName("Wingdings");
listStyle1.setName("liststyle1");
doc.getListStyles().add(listStyle1);
ListStyle listStyle2 = new ListStyle(doc, ListType.Bulleted);
listStyle2.getLevels().get(0).setBulletCharacter("\u0075");
listStyle2.getLevels().get(0).getCharacterFormat().setFontName("Wingdings");
listStyle2.setName("liststyle2");
doc.getListStyles().add(listStyle2);
ListStyle listStyle3 = new ListStyle(doc, ListType.Bulleted);
listStyle3.getLevels().get(0).setBulletCharacter("\u00b2");
listStyle3.getLevels().get(0).getCharacterFormat().setFontName("Wingdings");
listStyle3.setName("liststyle3");
doc.getListStyles().add(listStyle3);
ListStyle listStyle4 = new ListStyle(doc, ListType.Bulleted);
listStyle4 .getLevels().get(0).setBulletCharacter("\u00d8");
listStyle4 .getLevels().get(0).getCharacterFormat().setFontName("Wingdings");
listStyle4.setName("liststyle4");
doc.getListStyles().add(listStyle4);
//Add four paragraphs and apply list style separately
Paragraph p1 = section.getBody().addParagraph();
p1.appendText("Spire.Doc for .NET");
p1.getListFormat().applyStyle(listStyle1.getName());
Paragraph p2 = section.getBody().addParagraph();
p2.appendText("Spire.PDF for .NET");
p2.getListFormat().applyStyle(listStyle2.getName());
Paragraph p3 = section.getBody().addParagraph();
p3.appendText("Spire.XLS for .NET");
p3.getListFormat().applyStyle(listStyle3.getName());
Paragraph p4= section.getBody().addParagraph();
p4.appendText("Spire.Presentation for .NET");
p4.getListFormat().applyStyle(listStyle4.getName());
//Save to file
doc.saveToFile("SetBulletCharacter.docx", FileFormat.Docx);
}
}

This article demonstrates how to remove blank lines/empty paragraphs in a Word document by using Spire.Doc for Java.
Below is the sample document which contains many blank lines:

import com.spire.doc.*;
import com.spire.doc.documents.*;
public class removeBlankLines {
public static void main(String[] args) {
//Load the sample document
Document document = new Document();
document.loadFromFile("sample.docx");
//Traverse every section in the word document and remove the null and empty paragraphs
for (Object sectionObj : document.getSections()) {
Section section=(Section)sectionObj;
for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
if ((section.getBody().getChildObjects().get(i).getDocumentObjectType().equals(DocumentObjectType.Paragraph) )) {
String s= ((Paragraph)(section.getBody().getChildObjects().get(i))).getText().trim();
if (s.isEmpty()) {
section.getBody().getChildObjects().remove(section.getBody().getChildObjects().get(i));
i--;
}
}
}
}
//Save the document to file
String result = "removeBlankLines.docx";
document.saveToFile(result, FileFormat.Docx_2013);
}
}

Paragraph spacing and line spacing play a vital role in the readability and overall appearance of a document. If paragraphs are tightly packed together or lines are too closely spaced, it can strain the reader's eyes and make the content appear cluttered. Knowing how to set paragraph spacing and line spacing can help you create a visually pleasing and reader-friendly document. This article will demonstrate how to set paragraph spacing and line spacing in Word documents in Java using Spire.Doc for Java.
Install Spire.Doc for Java
First, 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.
<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>13.11.2</version>
</dependency>
</dependencies>
Set Paragraph Spacing in Word in Java
You can utilize the Paragraph.getFormat().setBeforeSpacing() and Paragraph.getFormat().setAfterSpacing() methods provided by Spire.Doc for Java to easily adjust the spacing before and after a paragraph. The detailed steps are as follows.
- Create an object of the Document class.
- Add a section to the document using Document.addSection() method.
- Add two paragraphs to the section using Section.addParagraph() methods.
- Set the spacing before and after the paragraphs using Paragraph.getFormat().setBeforeSpacing() and Paragraph.getFormat().setAfterSpacing() methods.
- 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.fields.TextRange;
import java.awt.*;
public class SetParagraphSpacing {
public static void main(String[] args){
// Create an object of the Document class
Document document = new Document();
// Add a section to the document
Section section = document.addSection();
// Add two paragraphs to the section
Paragraph para1 = section.addParagraph();
para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
TextRange textRange1 = para1.appendText("Spire.Doc for Java");
textRange1.getCharacterFormat().setTextColor(Color.BLUE);
textRange1.getCharacterFormat().setFontName("Calibri");
textRange1.getCharacterFormat().setFontSize(15);
Paragraph para2 = section.addParagraph();
TextRange textRange2 = para2.appendText("Spire.Doc for Java is a professional Word Java API specifically designed for developers to create, read, write, convert, and compare Word documents with fast and high-quality performance.");
textRange2.getCharacterFormat().setFontName("Calibri");
textRange2.getCharacterFormat().setFontSize(12);
// Set the spacing after the first paragraph
para1.getFormat().setAfterAutoSpacing(false);
para1.getFormat().setAfterSpacing(10);
// Set the spacing before and after the second paragraph
para2.getFormat().setBeforeAutoSpacing(false);
para2.getFormat().setBeforeSpacing(10);
para2.getFormat().setAfterAutoSpacing(false);
para2.getFormat().setAfterSpacing(10);
// Save the result file
document.saveToFile("SetParagraphSpacing.docx", FileFormat.Docx_2013);
document.close();
}
}

Set Line Spacing in Word in Java
To set the pacing between lines in a paragraph, you can use the Paragraph.getFormat().setLineSpacing() method. The detailed steps are as follows.
- Create an object of the Document class.
- Add a section to the document using Document.addSection() method.
- Add a paragraph to the section using Section.addParagraph() methods.
- Set the spacing between lines in the paragraph using Paragraph.getFormat().setLineSpacing() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.LineSpacingRule;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
public class SetLineSpacing {
public static void main(String[] args) {
// Create an object of the Document class
Document document = new Document();
// Add a section
Section section = document.addSection();
// Add a paragraph to the section
Paragraph para = section.addParagraph();
TextRange textRange = para.appendText("Spire.Doc for Java is a proven reliable MS Word API for Java which enables to perform many Word document processing tasks. Spire.Doc for Java supports Word 97-2003 /2007/2010/2013/2016/2019 and it has the ability to convert them to commonly used file formats like XML, RTF, TXT, XPS, EPUB, EMF, HTML and vice versa. Furthermore, it supports to convert Word Doc/Docx to PDF using Java, Word to SVG, and Word to PostScript in high quality.");
textRange.getCharacterFormat().setFontName("Calibri");
textRange.getCharacterFormat().setFontSize(12);
// Set line spacing rule
para.getFormat().setLineSpacingRule(LineSpacingRule.Multiple);
// Set line spacing value (The line spacing rule "Multiple" with value 18 sets the line spacing to "1.5 lines", value 12 sets the line spacing to "Single")
para.getFormat().setLineSpacing(18);
// Save the result file
document.saveToFile("SetLineSpacing.docx", FileFormat.Docx_2013);
document.close();
}
}

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.
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.
<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>13.11.2</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.Center);
//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);
}
}

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.
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.
<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>13.11.2</version>
</dependency>
</dependencies>
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
document.loadFromFile("Input.docx");
//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();
format.setLeftIndent(30);
//Get the third paragraph and set right indent
para = section.getParagraphs().get(2);
format = para.getFormat();
format.setRightIndent(30);
//Get the fourth paragraph and set first line indent
para = section.getParagraphs().get(3);
format = para.getFormat();
format.setFirstLineIndent(30);
//Get the fifth paragraph and set hanging indent
para = section.getParagraphs().get(4);
format = para.getFormat();
format.setFirstLineIndent(-30);
//Save the result document
document.saveToFile("SetParagraphIndents.docx", FileFormat.Docx_2013);
}
}
The following is the output Word document after setting paragraph indents:

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.