Indentation on line breaks

Hello,
i want to convert a String to a PDF document.
I use these Methods:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write(text);

doc.save(outputPdfPath, SaveFormat.PDF);

My String has got indentations in some paragraphs.
When the paragraph is to long to fit in one line, it breaks into a new line. The problem is, that the new line is now without indentation.

Is there a way to have the same indentation level in the new line break?

Here is my TestData:
LineIndentation.zip (62,6 KB)

@Sven2000 There is not left indent there is tab in your string. You can use actual left indent for paragraphs instead of tabs:

String text = "The autumn season is marked by its vibrant colors and crisp temperatures. Leaves change their hues and gently fall from the trees, painting the landscape in warm, golden tones." +
        "\n\n LINE INDENT:" +
        "\n\tDuring this time, many people are drawn to the outdoors to appreciate the beauty of the changing scenery. Long walks through the woods, the crunch of leaves underfoot, and the fresh, cool air are just a few of the pleasures that autumn brings." +
        "\n\nSome Text" +
        "\n\tBut it's not just nature that changes; the days grow shorter, and the evenings become longer.";

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
String[] paragraphs = text.split("\n");
for (String para : paragraphs)
{
    builder.getParagraphFormat().clearFormatting();
    if (para.startsWith("\t"))
    {
        builder.getParagraphFormat().setLeftIndent(doc.getDefaultTabStop());
    }
    builder.writeln(para.trim());
}

doc.save("C:\\Temp\\out.pdf", SaveFormat.PDF);

out.pdf (31.7 KB)

Thank you very much!
That helped me a lot

1 Like