首页 > 解决方案 > PDFlib - 在左上角而不是左下角使用“textflow”放置换行文本

问题描述

给定: a) 一段只能宽 10 厘米的长文本。高度是无限的,段落文本在到达右边距时应该换行;b) 带有 的页面topdown=true

我正在尝试使用add_textflow()and的组合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 抛出错误。

标签: pdflib

解决方案


首先,您的代码错过了$optionbegin_page_ext() 调用中的非可选参数。在您的情况下,您可能会使用

$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 章“坐标系”。


推荐阅读