How do I set the indentation of a Paragraph object in iText 8?

In iText 8, indentation of a Paragraph can be controlled using the setMarginLeft() and setMarginRight() method of the BlockElement class which is the super class of Paragraph.

Here is an example of how to indent a paragraph:

package org.kodejava.itext;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

public class ParagraphIndentation {
    public static void main(String[] args) throws Exception {
        PdfWriter writer = new PdfWriter("indentation.pdf");
        PdfDocument pdf = new PdfDocument(writer);

        try (Document document = new Document(pdf)) {
            String first = """
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, \
                    sed do eiusmod tempor incididunt ut labore et dolore magna \
                    aliqua. Ut enim ad minim veniam, quis nostrud exercitation \
                    ullamco laboris nisi ut aliquip ex ea commodo consequat. \
                    """;
            String second = """
                    Duis aute irure dolor in  reprehenderit in voluptate velit \
                    esse cillum dolore eu fugiat nulla pariatur. Excepteur sint \
                    occaecat cupidatat non proident, sunt in culpa qui officia \
                    deserunt mollit anim id est laborum.
                    """;

            Paragraph firstParagraph = new Paragraph(first);
            firstParagraph.setFirstLineIndent(60);
            firstParagraph.setMarginLeft(50);
            firstParagraph.setMarginRight(50);
            document.add(firstParagraph);

            Paragraph secondParagraph = new Paragraph(second);
            secondParagraph.setFirstLineIndent(60);
            secondParagraph.setMarginLeft(50);
            secondParagraph.setMarginRight(50);
            document.add(secondParagraph);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In this example, setMarginLeft() and setMarginRight() methods are used to set the left and right indentation of the paragraph. These methods accept a float value as the parameter for setting the left and right indentation respectively.

The setFirstLineIndent() method in the Paragraph class sets the indentation of the first line of a Paragraph in points. This option is commonly used in document formatting where the first line of a paragraph is typically indented more than the rest of the text in the paragraph.

For example, if you set paragraph.setFirstLineIndent(60);, the first line of the text in that paragraph would be indented 60 points from the left margin.

The generated PDF document looks like this:

Paragraph Indentation

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-core</artifactId>
    <version>8.0.2</version>
    <type>pom</type>
</dependency>

Maven Central

How do I set Paragraph indentation in iText?

To set paragraph indentation in the iText Paragraph object we can use the setFirstLineIndent(), setIndentationLeft() and setIndentationRight(). These methods accept a float value as a parameter.

The setFirstLineIndent() method set the first line indentation of a paragraph, while the setIndentationLeft() and setIndentationRight() methods set the left and the right indent of the paragraph. Let see an example below:

package org.kodejava.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ParagraphIndentationDemo {
    private static final String CONTENT = """
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, \
            sed do eiusmod tempor incididunt ut labore et dolore magna \
            aliqua. Ut enim ad minim veniam, quis nostrud exercitation \
            ullamco laboris nisi ut aliquip ex ea commodo consequat. \
            Duis aute irure dolor in  reprehenderit in voluptate velit \
            esse cillum dolore eu fugiat nulla pariatur. Excepteur sint \
            occaecat cupidatat non proident, sunt in culpa qui officia \
            deserunt mollit anim id est laborum.
            """;

    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("ParaIndentation.pdf"));
            document.open();

            Paragraph paragraph = new Paragraph();
            paragraph.add(new Chunk(ParagraphIndentationDemo.CONTENT));

            // Set paragraph's first line indent
            paragraph.setFirstLineIndent(75);

            // Set paragraph's left side indent
            paragraph.setIndentationLeft(50);

            // Set paragraph's right side indent
            paragraph.setIndentationRight(25);
            document.add(paragraph);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central