PDFlib-使用“文本流”在左上角而不是左下角放置换行文本

时间:2019-08-14 12:07:19

标签: pdflib

给出:a)一段长只有10cm的文本。高度是无限制的,段落文字到达右边距时应自动换行; b)带有topdown=true的页面。

我正在尝试使用add_textflow()fit_textflow()的组合来做到这一点。但是PDFlib将段落放置在左下角,而该段落的已知坐标是在左上角。

我的代码:

$p->begin_page_ext($width, $height);
$p->set_option("usercoordinates=true");
$p->set_option("topdown=true");

...

$tf = 0;
$tf = $p->add_textflow($tf, 'My loooong wrapping paragraph, 'fontname=Helvetica fontsize=10 encoding=unicode charref');
$result = $p->fit_textflow($tf, $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY, 'fitmethod=nofit');
$p->delete_textflow($tf);

问题: 我该怎么做才能将坐标提供为: $ p-> fit_textflow($ tf,$ topLeftX,$ topLeftY,$ lowerRightX,$ lowerRightY)?

我尝试将position={left top}添加到fit_textflow()选项,但是PDFlib引发错误。

1 个答案:

答案 0 :(得分:1)

首先,您的代码在begin_page_ext()调用中错过了非可选参数$option。您可以使用

$p->begin_page_ext($width, $height, "topdown=true");

因此您摆脱了额外的set_option()调用。

Textflow输出始终从fitbox的顶部开始(文本将放置的区域),在右边框后面不会写任何行。因此,您的要求是默认设置。

您可能会开始使用starter_textflow.php示例来获得如何使用它的第一印象(尤其是对于长文本,这不适合给定的fitbox)。 PDFlib食谱中的许多其他示例还显示了其他(更复杂的)方面:https://www.pdflib.com/pdflib-cookbook/textflow/

就您而言,您可以简单地使用:

$lowerLeftX = 0;
$lowerLeftY = $height;          // this is the page height
$upperRightX = 10 * 72 / 2.54;  // this is 10 cm in the default coordinate system
$upperRightY = 0;               // this is the top border of the page

$result = $p->fit_textflow($tf, $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY, 'fitmethod=nofit');

有关坐标系的详细信息,请参见PDFlib 9.2教程的第3.2.1节“坐标系”。

相关问题