How to set font color using PDF :: API2 Perl module?

I need to add color to some text in a PDF using PDF :: API2 - how to do this?

+4
source share
4 answers

You can set the text color by calling the fillcolor method before adding text:

 use PDF::API2; my $pdf = PDF::API2->new(); # Create a PDF my $font = $pdf->corefont('Helvetica'); # Add a font to the PDF my $page = $pdf->page(); # Create a page to hold your text my $text = $page->text(); # Create a graphics/text object $text->font($font, 12); # Set the font and size for your text $text->fillcolor('#FF0000'); # Set the text color $text->text('This text will be red.'); # Add your text 

Web-style color names will probably work fine in most cases, but you can give CMYK color instead of β€œ%” instead of β€œ#" and pass in four values ​​(for example, %00FF0000 for magenta).

PDF documentation :: API2 :: Content contains more detailed information about the various methods that will affect the $text object.

+5
source

According to PDF :: API2 :: Content, it looks like you are passing a hashref method for a text method (in PDF :: API :: Content :: Text object format).

Therefore, it should β€œwork” as follows (NB. I do not have a PDF :: API2 file installed here, so the code below is not verified):

 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; $txt->font( $font, 20 ); $txt->translate( 50, 800 ); $txt->text('Hello there!', { color => '#e6e6e6' } ); # <= hashref option $pdf->saveas( "file.pdf" ); $pdf->end(); 

Hope this helps?

+4
source

The only options supported by $txt->text are -indent, -underline and -strokecolor, although -strokecolor is only used in conjunction 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.

+2
source

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 the hex color to whatever you want.

+1
source

All Articles