【问题标题】:How do I set font colour with the PDF::API2 Perl module?如何使用 PDF::API2 Perl 模块设置字体颜色?
【发布时间】:2010-11-20 05:00:40
【问题描述】:

我需要使用 PDF::API2 为 PDF 文档中的某些文本添加颜色 - 我该怎么做?

【问题讨论】:

    标签: perl pdf fonts


    【解决方案1】:

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

    希望有帮助吗?

    【讨论】:

      【解决方案2】:

      $txt-&gt;text 支持的唯一选项是 -indent、-underline 和 -strokecolor,但 -strokecolor 仅与 -underline 结合使用来确定线条的颜色。

      使用$txt-&gt;fillcolor('colorname')$txt-&gt;fillcolor('#RRGGBB') 设置在fillcolor 命令之后写入的任何文本的颜色。

      【讨论】:

        【解决方案3】:

        使用类似以下的内容:

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

        显然,将十六进制颜色更改为您想要的任何颜色。

        【讨论】:

          【解决方案4】:

          可以在添加文字前调用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 样式的颜色名称可能会正常工作,但您可以通过使用“%”而不是“#”并传递四个值(例如,%00FF0000 代表洋红色)来提供 CMYK 颜色。

          PDF::API2::Content 文档详细介绍了影响$text 对象的各种方法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-09-30
            • 1970-01-01
            • 1970-01-01
            • 2014-04-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多