文本行火柴盒的透明背景色(fillcolor)

时间:2019-04-04 12:44:42

标签: php pdflib

我正在用pdflib和php创建名片。我正在将Mac OS的PDFLib 9.0.6p4与PHP版本5.6一起使用。有一个背景图像,上面显示了文本。为了使其更具可读性,我想为文本添加一些背景色。设法用火柴盒和fillcolor做到这一点。火柴盒是否可以使用透明的填充色?

已经尝试将透明的rect()作为文本的背景而不是火柴盒,但是它没有用,因为在打印文本之前我不知道其高度和宽度。

    ...
    $textline = "Max Mustermann";
    $p->save();
    $gstate = $p->create_gstate("opacityfill=.8");
    /* Apply the extended graphics state */
    $p->set_gstate($gstate); 
    # Place the text in a box on the top right
    $optlist = "position={top left} showborder matchbox={ boxheight={fontsize descender} borderwidth=0.3 offsetleft=-2 offsetright=2 offsetbottom=-2 fillcolor=green}";
    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);   # sample text
    $p->fit_textline($optlist, $xt, $y + 3, "fontsize=12"); # description
    $p->restore();
    ...

不幸的是,这并没有为文本带来预期的透明背景色效果。

1 个答案:

答案 0 :(得分:0)

您非常接近:您应将 gstate 句柄添加到火柴盒选项列表中(简化的情况):

$gstate = $p->create_gstate("opacityfill=.8");
$optlist = "matchbox={ fillcolor=green gstate=$gstate}";
$p->fit_textline($textline, $x, $y-=$yoff, $optlist);