PDFBox 布局:向 StyledText 添加换行符

PDFBox Layout: Add Line Break to StyledText

我正在使用 pdfbox-layout,我想向 StyledText 元素添加换行符,但无法做到。

如果带样式的文本包含换行符“\n”,我会收到以下错误消息:

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

但是我在文档中找不到说明如何使用 TextFragment.LINEBREAK 的示例。

有人对此有示例或替代解决方案吗?

解决方案:

我找到了解决方法。每次需要换行时,您都需要创建一个 NewLine Textfragment 并将其添加到 TextFlow。这是一个例子:

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);

解决方案:

我找到了解决方法。每次需要换行时,您都需要创建一个 NewLine Textfragment 并将其添加到 TextFlow。这是一个例子:

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);