layoutpdfboxline-breaksstyledtext

PDFBox Layout: Add Line Break to StyledText


I'm using pdfbox-layout and I want to add line breaks to a StyledText Element but can't manage to do it.

If a styled text contains a line break "\n", I get the following error message:

"StyledText must not contain line breaks, use TextFragment.LINEBREAK for that"

But I can't find an example in the documentation that shows how use TextFragment.LINEBREAK.

Does anybody have an example or a alternative solution for this?

SOLUTION:

I found a solution how to do it. You need to create a NewLine Textfragment and add it to the TextFlow every time you need a linebreak. Here is an example:

NewLine linebreak = new NewLine(fontSize);
TextFlow textflow = new TextFlow();
textflow.add("first line");
textflow.add(linebreak);
textflow.add("second line");
textflow.add(linebreak);
textflow.add("third line");
textflow.drawText(contentStream, posX, posY, Alignment.Left, drawListener);

Solution

  • I found a solution how to do it. You need to create a NewLine Textfragment and add it to the TextFlow every time you need a linebreak. Here is an example:

    NewLine linebreak = new NewLine(fontSize);
    TextFlow textflow = new TextFlow();
    textflow.add("first line");
    textflow.add(linebreak);
    textflow.add("second line");
    textflow.add(linebreak);
    textflow.add("third line");
    textflow.drawText(contentStream, posX, posY, Alignment.Left, drawListener);