如何使用 PDFBox drawString 插入换行符
我必须制作一个带有表格的 PDF。到目前为止它工作正常,但现在我想添加一个包装功能。所以我需要插入换行符。
contentStream.beginText();
contentStream.moveTextPositionByAmount(x, y);
contentStream.drawString("Some text to insert into a table.");
contentStream.endText();
我想在“插入”之前添加“\n
”。我尝试了“\u000A
”,它是换行符的十六进制值,但 Eclipse 显示了一个错误。
是否可以使用drawString添加换行符?
I have to make a PDF with a Table. So far it work fine, but now I want to add a wrapping feature. So I need to insert a Linefeed.
contentStream.beginText();
contentStream.moveTextPositionByAmount(x, y);
contentStream.drawString("Some text to insert into a table.");
contentStream.endText();
I want to add a "\n
" before "insert". I tried "\u000A
" which is the hex value for linefeed, but Eclipse shows me an error.
Is it possible to add linefeed with drawString?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PDF 格式允许换行,但 PDFBox 没有内置换行功能。
要在 PDF 中使用换行符,您必须使用
TL
运算符定义要使用的行距。T*
运算符进行换行。'
运算符将给定的文本写入下一行。 (有关更多详细信息,请参阅 PDF 规范的“文本”一章。没有那么多。)这里有两个代码片段。两者的作用相同,但第一个代码段使用
'
,第二个代码段使用T*
。使用
T*
进行换行:要获取字体的高度,您可以使用以下命令:
您可能希望将其与某些行间距系数相乘。
The PDF format allows line breaks, but PDFBox has no build in feature for line breaks.
To use line breaks in PDF you have to define the leading you want to use with the
TL
-operator. TheT*
-operator makes a line break. The'
-operator writes the given text into the next line. (See PDF-spec for more details, chapter "Text". It´s not that much.)Here are two code snippets. Both do the same, but the first snippet uses
'
and the second snippet usesT*
.Use
T*
for line break:To get the height of the font you can use this command:
You might want to multiply it whit some line pitch factor.
pdf 格式不知道换行符。您必须使用 moveTextPositionByAmount 拆分字符串并将文本位置移动到下一行。
这不是一个特殊的“pdfbox-feature”,它是由于pdf格式定义造成的;所以drawString没有办法,也没有其他方法可以调用来支持换行。
The pdf format doesn't know line breaks. You have to split the string and move the text position to the next line, using moveTextPositionByAmount.
This is not a special "pdfbox-feature", it is due to the pdf format definition; so there is no way for drawString and there are also no other methods to be called that support linefeeds.
由于 PDF 模型通常不是当前任务的最佳模型,因此为其编写一个包装器来添加对您案例中“缺失”的任何内容的支持通常是有意义的。对于阅读和写作来说都是如此。
Because the PDF model often isn't the best model for the task at hand, it often makes sense to write a wrapper for it that adds support for whatever's "missing" in your case. This is true for both reading and writing.