pdflib

PDFlib - setting stroke and fill opacity (transparency)


Is it possible to set value of alpha channel when providing fill and stroke colors in PDFlib?

$p->setlinewidth(20);
$p->setcolor('fill', 'rgb', 1, 0, 0, null);
$p->setcolor('stroke', 'rgb', 0, 1, 0, null);
$p->rect(0, 0, 100, 100);
$p->fill_stroke();

Is it possible to make rectangle's red fill and thick green border to be semi-transparent?


Solution

  • Is it possible to make rectangle's red fill and thick green border to be semi-transparent?

    sure, please use GState for this task. You find a complete sample code within the PDFlib cookbook: Transparent Graphics

    
        /* Save the current graphics state. The save/restore of the current
         * state is not necessarily required, but it will help you get back to
         * a graphics state without any transparency.
         */
    
        $gstate = $p->create_gstate("opacityfill=.5 opacitystroke=.5");
        $p->save();
          $p->set_gstate($gstate);
          $p->setlinewidth(20);
          $p->setcolor('fill', 'rgb', 1, 0, 0, null);
          $p->setcolor('stroke', 'rgb', 0, 1, 0, null);
          $p->rect(0, 0, 100, 100);
          $p->fill_stroke();
        $p->restore();
    

    For a powerful path generation, you might use the Path object. See PDFlib 9.2 Documentation as well the samples within PDFlib Cookbook - path objects.