PDFlib-控制文本颜色,文本背景和文本笔触的背景和不透明度

时间:2019-08-27 20:04:28

标签: pdflib

我正在尝试分别设置背景值和文本颜色,文本背景和笔触(轮廓)的不透明度。

下面的代码

$p->save();
$p->setfont($font, 240);
$p->set_gstate($p->create_gstate('opacityfill=1 opacitystroke=1')); // Both fill and stroke are opaque
$p->set_graphics_option('fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->fit_textline('QfjIL', 30, 30, 'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0}} charspacing=0 textrendering=2 strokewidth=10 position={left top}');
$p->restore();

导致:

enter image description here

黄色背景,蓝色字母笔划和绿色字母填充不透明-符合预期。

gstate添加填充和描边的不透明度为:

$p->save();
$p->setfont($font, 240);
$p->set_gstate($p->create_gstate('opacityfill=0.3 opacitystroke=0.3'));
$p->set_graphics_option('fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->fit_textline('QfjIL', 30, 30, 'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0}} charspacing=0 textrendering=2 strokewidth=10 position={left top}');
$p->restore();

使用相同的不透明度得出所有背景,填充和描边:

enter image description here

问题

如何分别控制文本背景的不透明度(黄色),字母笔画的不透明度(蓝色)和字母填充的不透明度(绿色)?

1 个答案:

答案 0 :(得分:1)

这是预期的结果,因为您为所有填充和描边内容指定了图形状态。

您应该只为文本设置不透明度gstate,为火柴盒设置纯gstate。

$p->save();
$gstate_solid = $p->create_gstate('opacityfill=1 opacitystroke=1');
$gstate = $p->create_gstate('opacityfill=0.3 opacitystroke=0.3');
$p->fit_textline('QfjIL', 30, 30, 
   'fontname=NotoSerif-Regular encoding=unicode fontsize=240 ' . 
   'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0} gstate=' . $gstate_solid . '} '
   'charspacing=0 textrendering=2 strokewidth=10 position={left top} gstate=' . $gstate . 'fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->restore();

这给出了以下结果,我想这是预期的结果。

enter image description here

您可以在PDFlib 9.2 API参考的第6.2章“匹配框”中找到有关匹配框选项的所有详细信息。