In this article, we will demonstrate how to indent paragraphs in Word in Java applications usingSpire.Doc for Java. Spire.Doc for Java offersParagraphFormat.setLeftIndent(float value) method to set the paragraph left indents andParagraphFormat.setRightIndent(float value) for right indents. TheParagraphFormat.setFirstLineIndent(float value) method can be used to set both hanging and first line indents. Negative value for this property is for the hanging indent and positive value for the first line indent of the paragraph.
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 downloadedfrom 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>4.12.7</version></dependency></dependencies>
Using the code
The following are the steps to set paragraph indents in Word:
- Create a Document instance.
- Load a Word document usingDocument.loadFromFile() method.
- Get the desired section by its index usingDocument.getSections.get() method.
- Get the desired paragraph by index usingSection.getParagraphs.get() method.
- Get the ParagraphFormat object usingParagraph.getFormat() method.
- Call the methods to set paragraph indents with the object.
- Save the result document usingDocument.saveToFile() method.
importcom.spire.doc.Document;importcom.spire.doc.FileFormat;importcom.spire.doc.Section;importcom.spire.doc.documents.Paragraph;importcom.spire.doc.formatting.ParagraphFormat;publicclassTest{publicstaticvoidmain(String[]args)throwsException{//Create a Document instanceDocumentdocument=newDocument();//Load a Word documentdocument.loadFromFile("Sample.docx");//Get the first sectionSectionsection=document.getSections().get(0);//Get the first paragraph and set left indent, right indentParagraphpara=section.getParagraphs().get(0);ParagraphFormatformat=para.getFormat();format.setLeftIndent(30);format.setRightIndent(50);//Get the second paragraph and set first line indentpara=section.getParagraphs().get(1);format=para.getFormat();format.setFirstLineIndent(30);//Get the third paragraph and set hanging indentpara=section.getParagraphs().get(2);format=para.getFormat();format.setFirstLineIndent(-30);//Save the result documentdocument.saveToFile("SetParagraphIndents.docx",FileFormat.Docx_2013);}}
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse