ksl*_*ksl 6 java pdf pdf-generation pdfbox
我刚开始使用Apache PDFBox并且正在尝试我发现的各种示例.
但是,在添加文本时,我无法找到一种简单的方法来移动到下一行.
例如
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();
Run Code Online (Sandbox Code Playgroud)
要在下面添加另一行文本,我必须反复尝试y in的值,moveTextPositionByAmount
直到它没有覆盖前一行.
是否有更直观的方法来确定下一行的坐标是什么?
TIA
PDFBox API允许生成低级内容.这意味着你必须自己做(而且你也可以这么做)大部分布局工作,其中包括决定向下移动到下一个基线的程度.
该距离(在此背景下称为领先)取决于许多因素:
该标准被布置成使得文本的紧密间隔的线的标称高度为1个单位在尺寸1.绘制的字体因此,通常将使用1..1.5倍的字体大小的领先除非材料就行超越它.
顺便说一句,如果你必须经常以相同的数量转发到下一行,你可以使用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.");
Run Code Online (Sandbox Code Playgroud)
PS:它看起来moveTextPositionByAmount
将在2.0.0版本中弃用并被替换为newLineAtOffset
.
PPS:正如OP在评论中指出的那样,
没有名为setLeading的PDPageContentStream方法.我正在使用PDFBox版本1.8.8.
实际上,我正在研究当前的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*");
}
Run Code Online (Sandbox Code Playgroud)
可以轻松实现使用appendRawCommands((float) leading); appendRawCommands(" TL");
和等效的外部辅助方法appendRawCommands("T*");
归档时间: |
|
查看次数: |
11406 次 |
最近记录: |