簡體   English   中英

如何使用PDF :: API2 Perl模塊設置字體顏色?

[英]How do I set font colour with the PDF::API2 Perl module?

我需要使用PDF :: API2為PDF文檔中的某些文本添加顏色 - 我該怎么做?

您可以在添加文本之前調用fillcolor方法來設置文本顏色:

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樣式的顏色名稱可能會正常工作,但您可以使用“%”而不是“#”來傳遞CMYK顏色並傳遞四個值(例如,對於洋紅色%00FF0000%00FF0000 )。

PDF :: API2 :: Content文檔提供了有關影響$text對象的各種方法的更多詳細信息。

根據PDF :: API2 :: Content,它看起來像是將hashref選項傳遞給text方法(在PDF :: API :: Content :: Text對象上)。

所以它“應該”像這樣工作(注意。我沒有安裝PDF :: API2,所以下面的代碼未經測試):

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();

希望有幫助嗎?

$txt->text支持的唯一選項是-indent,-underline和-strokecolor,但-strokecolor僅與-underline結合使用以確定行的顏色。

使用$txt->fillcolor('colorname')$txt->fillcolor('#RRGGBB')設置fillcolor命令后寫入的任何文本的顏色。

使用類似以下內容:

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',
);

顯然,將十六進制顏色更改為您想要的顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM