ademmler has asked for the wisdom of the Perl Monks concerning the following question:

hi,

I am using PDF::API2 and I try to make a Text having a green filling and a red stroke around the text. But I cant figure out how to get this.
Here is my test script. Hoping somebody has the right idea.

thx to all, alexander

#!/usr/bin/perl

# Sample code taken from: http://rick.measham.id.au/pdf-api2/
# Thanks to the author
#

use strict;
use warnings;

use PDF::API2;

use constant mm => 25.4 / 72;
use constant in => 1 / 72;
use constant pt => 1;

my $pdf = PDF::API2->new( -file => "$0.pdf" );

my $page = $pdf->page;
$page->mediabox( 105 / mm, 35 / mm );

my %font = (
Helvetica => {
Bold => $pdf->corefont( 'Helvetica-Bold', -encoding => 'latin1' ),
Roman => $pdf->corefont( 'Helvetica', -encoding => 'latin1' ),
Italic => $pdf->corefont( 'Helvetica-Oblique', -encoding => 'latin1' ),
},
);

my $headline_text = $page->text;
$headline_text->font( $font{'Helvetica'}{'Bold'}, 18 / pt );
$headline_text->fillcolor('green');
$headline_text->strokecolor('red');
$headline_text->linewidth(0.5 / mm);
$headline_text->fillstroke('1');
$headline_text->stroke;
$headline_text->translate( 95 / mm, 15 / mm );
$headline_text->text_right('My Headline');

$pdf->save;
$pdf->end();

Replies are listed 'Best First'.
Re: PDF::API2 + Text with stroke
by almut (Canon) on Oct 28, 2009 at 22:55 UTC

    You have to set the text rendering mode to 2  (2=fill+stroke — default is 0=fill only):

    ... my $headline_text = $page->text; $headline_text->font( $font{'Helvetica'}{'Bold'}, 18 / pt ); $headline_text->fillcolor('green'); $headline_text->strokecolor('red'); $headline_text->linewidth(0.2 / mm); $headline_text->render(2); # <-- $headline_text->translate( 95 / mm, 15 / mm ); $headline_text->text_right('My Headline'); ...

    Yes, the docs are a little terse...: "$rendering = $txt->render $rendering" — so you're not to blame for not finding it :)

    (See also section 5.2.5 "Text Rendering Mode" and Table 5.3 (p. 402) in the PDF Reference v1.7.)