pdflib - 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 抛出错误。
解决方案
首先,您的代码错过了$option
begin_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 章“坐标系”。
推荐阅读
- python - 如何根据熊猫中的另一列数组对一列数组进行排序?
- groovy - 脚本模式下的 Katalon 动态 xpath
- r - 我可以使用 Sample.split 函数在基于某些字段进行过滤时拆分数据吗?
- for-loop - 在 v-tabs 中使用 v-for 进行三级迭代
- excel - “此时无法进入中断模式”- 运行时错误“1004”:
- javascript - 需要从两个符号之间删除空格
- php - Spatie/pdf-to-image 获取图像数量总是返回 0
- java - SQL 语句执行错误说'_HP_KYCMS18837P51144'附近的sunataxt 不正确
- python - 选择特定列表列表的多个索引
- c# - 如何查找具有开头模式并包含模式的字符串