文本行匹配框的透明背景颜色(填充颜色)

Transparent background-color (fillcolor) for textline matchboxes

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

我已经尝试将透明的 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();
    ...

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

你非常接近:你应该将 gstate 句柄添加到匹配框选项列表(简化情况):

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