Madhan Madhan - 4 months ago 18
Perl Question

Font color property in PDF using perl PDF::Create

How to set the font-color property when creating PDF through the Perl module PDF::Create? The below is the code snippet I am using. How can I set the font color property here?

use warnings;
use strict;

use PDF::Create;
use PDF::Image::JPEG;

print "PDF with Image\n";

#Create
my $pdf = new PDF::Create(
'filename' => "./image_embed_test.pdf",
'Version' => 1.2,
'PageMode' => 'UseNone',
'Author' => 'Madhan',
'Title' => 'My PDF',
);

# Prepare 2 fonts
my $f1 = $pdf->font('Subtype' => 'Type1',
'Encoding' => 'WinAnsiEncoding',
'BaseFont' => 'Helvetica');

my $f2 = $pdf->font('Subtype' => 'Type1',
'Encoding' => 'WinAnsiEncoding',
'BaseFont' => 'Helvetica-Bold');

my $f3 = $pdf->font('Subtype' => 'Type1',
'Encoding' => 'WinAnsiEncoding',
'BaseFont' => 'Helvetica-Bold');


my $root = $pdf->new_page('MediaBox' => [ 0, 0, 612, 792 ]);

my @page;
$page[1]=$root->new_page;

my $jpg1 = $pdf->image('./logo1.jpg');
$page[1]->image( 'image' => $jpg1,
'xscale' => 0.5,
'yscale' => 0.5,
'xpos' => 56,
'ypos' => 698);

$page[1]->stringc($f2, 8, 484, 738, "Add line 1");
$page[1]->stringc($f2, 8, 521, 718, "City");
$page[1]->stringc($f2, 8, 530, 698, "Phone");
$page[1]->stringc($f3, 8, 530, 698, "myid@example.com");

$pdf->close;


In the
$f3
for the mail id link I need to set the color as blue.

Answer

You need to use the setrgbcolor method from PDF::Create::Page.

setrgbcolor($r, $g, $b)
Sets the fill colors used for normal text or filled objects.

It looks like you need to set that before you run the stringc operation at it will just set the color for all subsequent operations. So you turn it to blue, write your blue text, and turn it back to black (or default).

$page[$num]->setrgbcolor(1, 0, 0); # blue
$page[$num]->stringc($f3, 8, 530, 698, "myid@example.com");
$page[$num]->setrgbcolor(0, 0, 0); # black

Directly below setrgbcolor there is setrgbcolorstroke, which explains that the parameters are values between 0 and 1.

Comments