PDFlib - 使用 "textflow" 将换行文本放在左上角而不是左下角

PDFlib - place wrapping text using "textflow" in top-left corner instead of bottom-left

给定: 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 抛出错误。

首先,您的代码在 begin_page_ext() 调用中遗漏了非可选参数 $option。在您的情况下,您可以使用

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

所以你摆脱了额外的 set_option() 调用。

Textflow 输出始终从限定框的顶部开始(放置文本的区域),右边框后面不会写任何行。所以你的要求是默认的。

您可能会开始使用 starter_textflow.php 示例以获得如何使用它的第一印象(尤其是对于不适合给定限定框的长文本)。 PDFlib cookbook 中的许多其他示例也显示了更多(更复杂)的方面: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 章"Coordinate Systems"。