In Word, Paragraph Indents mainly consist of Left Indent, Right Indent, First Line Indent and Hanging Indent. The Left Indent and the Right Indent control the left and right sides of the entire paragraph respectively, the First Line Indent controls the first line of the paragraph and the Hanging Indent controls the rest of the paragraph except for the first line. This article will show you how to set the four different types of Paragraph Indents for a Word document using Java codes.
DEPENDENCY
First of all, you need to download the product package of Free Spire.Doc for Java from the link, and manually add the Spire.Doc.jar file as a dependency in the Java program. If you use Maven, you can easily import the JAR file in your application by adding the following code to the pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
USING THE CODE
Free Spire.Doc for Java supports setting four different types of Paragraph Indents for a Word document using methods provided by the ParagraphFormat class. Below you can see the corresponding steps used to carry out the operations.
l Create a Document instance
and load a sample Word document using Document.loadFromFile()
method.
l Get a specific section of the file using Document.getSections.get() method and find the desired paragraph using
Section.getParagraphs.get() method.
l Get the paragraph format using Paragraph.getFormat()
method.
l Call the methods provided by the ParagraphFormat
class to set paragraph indents.
l Save the document using Document.saveToFile()
method.
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 ParagraphIndents {
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 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 paragraph and set right indent
para = section.getParagraphs().get(2);
format = para.getFormat();
format.setRightIndent(30);
//Get the 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 document to another file
document.saveToFile("output/SetParagraphIndents.docx", FileFormat.Docx_2013);
}
}
No comments:
Post a Comment