Once upon a time I had the same problem, which I then put away for 'later', but your request made me look it up.

The method for it is actually quite simple, however due to the level of documentation for PDF::API2, nobody knows how :)

Let's start by creating the document, add an A4-page, create text and gfxhandlers and add a font:
my $pdf = PDF::API2->new( -file => "Test.pdf" ); my $page1 = $pdf->page; $page1->mediabox (210/mm, 297/mm); my $gfx = $page1->gfx; my $text = $page1->text; my %fonts = ( Helvetica => { Roman => $pdf->corefont( 'Helvetica', -encoding => 'latin1' ), } );
Then we'll make some Extended Graphics states...One for everything we want transparent, and one for the non-transparent states.
my $EGTransparent = $pdf->egstate(); my $EGNormal = $pdf->egstate(); $EGTransparent->transparency(0.5); $EGNormal->transparency(0);
Suppose we now want to write some transparent text:
$text->egstate($EGTransparent); $text->font($fonts{Helvetica}{Roman}, 16/pt); $text->translate(31/mm,250/mm); $text->text("This is a Test, just so you know", undef); $text->egstate($EGNormal);
And add a transparent image to write transparent text over:
$gfx->egstate($EGTransparent); $gfx->image($pdf->image_tiff("Image.tif"), 1/mm, 148/mm, 137/mm, 137/m +m); $gfx->egstate($EGNormal);
And then add some non-transparent text, to show the difference:
$text->translate(31/mm,200/mm); $text->text("This is a Test, just so you know", undef);
And save our result...of course :)
$pdf->save;
All done.

In reply to Re: PDF::API2 and opacity by Neighbour
in thread PDF::API2 and opacity by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.