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. |