Independentsoft
Any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Paragraph indentation
The following example shows you how to set paragraph indentation.
import com.independentsoft.office.word.Indentation; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.ParagraphProperties; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.WordDocument; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); // Paragraph with left indentation Run run1 = new Run(); run1.addText("This paragraph has left indentation. The entire paragraph is indented from the left margin."); Paragraph paragraph1 = new Paragraph(); paragraph1.add(run1); Indentation indent1 = new Indentation(); indent1.setLeft(720); // 720 twips = 0.5 inch ParagraphProperties props1 = new ParagraphProperties(); props1.setIndentation(indent1); paragraph1.setParagraphProperties(props1); // Paragraph with first line indentation Run run2 = new Run(); run2.addText("This paragraph has first line indentation. Only the first line is indented, which is common for book paragraphs."); Paragraph paragraph2 = new Paragraph(); paragraph2.add(run2); Indentation indent2 = new Indentation(); indent2.setFirstLine(720); // 720 twips = 0.5 inch ParagraphProperties props2 = new ParagraphProperties(); props2.setIndentation(indent2); paragraph2.setParagraphProperties(props2); // Paragraph with hanging indentation Run run3 = new Run(); run3.addText("This paragraph has hanging indentation. The first line starts at the margin but subsequent lines are indented. This is often used for bibliographies."); Paragraph paragraph3 = new Paragraph(); paragraph3.add(run3); Indentation indent3 = new Indentation(); indent3.setLeft(720); indent3.setHanging(720); // Creates hanging effect ParagraphProperties props3 = new ParagraphProperties(); props3.setIndentation(indent3); paragraph3.setParagraphProperties(props3); // Paragraph with both left and right indentation Run run4 = new Run(); run4.addText("This paragraph has both left and right indentation. It appears as a block quote, indented from both margins."); Paragraph paragraph4 = new Paragraph(); paragraph4.add(run4); Indentation indent4 = new Indentation(); indent4.setLeft(720); indent4.setRight(720); ParagraphProperties props4 = new ParagraphProperties(); props4.setIndentation(indent4); paragraph4.setParagraphProperties(props4); doc.getBody().add(paragraph1); doc.getBody().add(paragraph2); doc.getBody().add(paragraph3); doc.getBody().add(paragraph4); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*