tags:

views:

327

answers:

3

I need to add colour to some text in a PDF document using PDF::API2 - how do I do that?

+1  A: 

According PDF::API2::Content it looks like you pass hashref option to text method (on a PDF::API::Content::Text object).

So it "should" work like this (NB. I don't have PDF::API2 installed here so code below is untested):

use PDF::API2;
use PDF::API2::Util;

my $pdf = PDF::API2->new;

my $font = $pdf->corefont('Helvetica',-encode=>'latin1');
my $page = $pdf->page;
$page->mediabox( 80, 500 );

my $txt = $page->text;
my $txt->font( $font, 20 );

$txt->translate( 50, 800 );
$txt->text('Hello there!', { color => '#e6e6e6' } );  # <= hashref option

$pdf->saveas( "file.pdf" );
$pdf->end();

Hope that helps?

/I3az/

draegtun
A: 

The only options that $txt->text supports are -indent, -underline and -strokecolor, though -strokecolor is only used in combination with -underline to determine the color of the line.

Use $txt->fillcolor('colorname') or $txt->fillcolor('#RRGGBB') to set the color of any text written after the fillcolor command.

A: 

Use something like the following:

my $margin = $x; #co-ordinates for page
my $margin = $y; #co-ordinates for page

my $caption = 'blah blah blah';
my $font=$pdf->corefont('Helvetica-Bold',-encode=>'latin1');
my $font_size = 12;

my $page = $pdf->openpage($pageNum);
my $gfx = $page->gfx;

$gfx->textlabel($margin,$y_pos, $font,$font_size,$caption,
   -color => '#5E5E5E',
);

And obviously change hex colour to whatever you want.

Jamie Nicholson