1

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);
Braunbaer
  • 53
  • 7
  • It's PDFBox respectively an extension of it -> https://github.com/ralfstuckert/pdfbox-layout – Braunbaer Aug 10 '18 at 11:36
  • I made clearer that you are looking for a solution in the context of that add-on to PDFBox. – mkl Aug 10 '18 at 13:52

1 Answers1

3

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);
codrelphi
  • 1,075
  • 1
  • 7
  • 13
Braunbaer
  • 53
  • 7