java 使用 Apache PDFBox 添加文本时如何移至下一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27837231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move to the next line when adding text using Apache PDFBox
提问by ksl
I've just started using Apache PDFBox and been experimenting with various examples I've found.
我刚刚开始使用 Apache PDFBox 并尝试了我发现的各种示例。
However, I haven't been able to find an easy way to move to the next line when adding text.
但是,在添加文本时,我一直无法找到一种简单的方法来移动到下一行。
E.g.
例如
PDPageContentStream content = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA;
content.beginText();
content.setFont(font, 12);
content.moveTextPositionByAmount(x, y);
content.drawString("Some text.");
content.endText();
To add another line of text underneath I had to repeatedly experiment with the value of y in moveTextPositionByAmount
until it wasn't overwriting the previous line.
为了在下面添加另一行文本,我不得不反复试验 y in 的值,moveTextPositionByAmount
直到它没有覆盖前一行。
Is there a more intuitive way to figure out what the coordinates of the next line are?
有没有更直观的方法来算出下一行的坐标是什么?
TIA
TIA
回答by mkl
The PDFBox API allows low-level content generation. This implies that you have to do (but also that you are enabled to do) much of the layout work yourself, among that deciding how much to move down to get to the next baseline.
PDFBox API 允许生成低级内容。这意味着您必须(但也有能力)自己完成大部分布局工作,其中包括决定向下移动多少才能到达下一个基线。
That distance (called leadingin this context) depends on a number of factors:
该距离(在这种情况下称为领先)取决于许多因素:
- the font size used (obviously)
- how tightly or loosely spaced the text shall appear
- the presence of elements on the lines involved positioned outside the regular line, e.g. superscripts, subscripts, formulas, ...
- 使用的字体大小(显然)
- 文本应显示的紧密或松散的间距
- 所涉及的行上存在的元素位于常规行之外,例如上标、下标、公式……
The standard is arranged so that the nominal height of tightly spaced lines of text is 1 unitfor a font drawn at size 1. Thus, usually you will use a leading of 1..1.5 times the font size unless there is material on the line reaching beyond it.
该标准被布置成使得文本的紧密间隔的线的标称高度为1个单位在尺寸1.绘制的字体因此,通常将使用一个领先1..1.5倍的字体大小的,除非有上线材料超越它。
BTW, if you have to forward to the next line by the same amount very often, you can use the combination of the PDPageContentStream
methods setLeading
and newLine
instead of moveTextPositionByAmount
:
顺便说一句,如果您必须经常以相同的数量转发到下一行,则可以使用这些PDPageContentStream
方法的组合,setLeading
而newLine
不是moveTextPositionByAmount
:
content.setFont(font, 12);
content.setLeading(14.5f);
content.moveTextPositionByAmount(x, y);
content.drawString("Some text.");
content.newLine();
content.drawString("Some more text.");
content.newLine();
content.drawString("Still some more text.");
PS: It looks like moveTextPositionByAmount
will be deprecated in the 2.0.0 version and be replaced by newLineAtOffset
.
PS:看起来moveTextPositionByAmount
在 2.0.0 版本中会被弃用并被newLineAtOffset
.
PPS: As the OP indicates in a comment,
PPS:正如 OP 在评论中指出的那样,
There is no PDPageContentStream method called setLeading. I'm using PDFBox version 1.8.8.
没有称为 setLeading 的 PDPageContentStream 方法。我正在使用 PDFBox 版本 1.8.8。
Indeed, I was looking at the current 2.0.0-SNAPSHOT development version. They are currently implemented like this:
事实上,我正在查看当前的 2.0.0-SNAPSHOT 开发版本。它们目前是这样实现的:
/**
* Sets the text leading.
*
* @param leading The leading in unscaled text units.
* @throws IOException If there is an error writing to the stream.
*/
public void setLeading(double leading) throws IOException
{
writeOperand((float) leading);
writeOperator("TL");
}
/**
* Move to the start of the next line of text. Requires the leading to have been set.
*
* @throws IOException If there is an error writing to the stream.
*/
public void newLine() throws IOException
{
if (!inTextMode)
{
throw new IllegalStateException("Must call beginText() before newLine()");
}
writeOperator("T*");
}
One can easily implement external helper methods doing the equivalent using appendRawCommands((float) leading); appendRawCommands(" TL");
and appendRawCommands("T*");
可以很容易地实现外部辅助方法,使用appendRawCommands((float) leading); appendRawCommands(" TL");
和appendRawCommands("T*");
回答by Giovanni Contreras
add a new line with offset in y axis like this
像这样在 y 轴上添加一个偏移量的新行
PDPageContentStream content = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA;
content.beginText();
content.setFont(font, 12);
// by default y = 0 pdf text start in the left bottom corner
// so you may need to put y = 700 or something to see the new line below
content.moveTextPositionByAmount(x, y);
content.drawString("Some text.");
content.newLineAtOffset(0, -15);
content.drawString("some text ");
content.endText();