Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

How to Indent a Paragraph in Word in Java

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>
Enter fullscreen modeExit fullscreen mode

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);}}
Enter fullscreen modeExit fullscreen mode

Image description

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Your Office Development Master
  • Location
    Chengdu, SiChuan, China
  • Work
    Dev at E-iceblue
  • Joined

More fromE-iceblue Product Family

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp