java设置pdfbox设置文档大小_java – 使用Apache PDFBox添加文本时如何移动到下一行...

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

PS:看起来moveTextPositionByAmount将在2.0.0版本中弃用,并被newLineAtOffset取代.

PPS:正如OP在评论中指出的那样,

There is no PDPageContentStream method called setLeading. I’m using PDFBox version 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*");

}

可以使用appendRawCommands((float)leading)轻松实现使用等效的外部辅助方法; appendRawCommands(“TL”);和appendRawCommands(“T *”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值