代码之家  ›  专栏  ›  技术社区  ›  Space

如何使用PDF::API2::Lite添加带有图像的页眉、页脚?

  •  5
  • Space  · 技术社区  · 16 年前

    可以添加页眉(带有文本和一幅图像)和页脚(带有页码)和图像吗。我写了下面的代码来创建一个显示png图像的PDF文档。

    如果这可以通过其他模块轻松完成,请建议。非常感谢您对示例代码的回复。

    use strict;
    use PDF::API2::Lite;
    use Getopt::Long;
    
    my $outfile;
    my $path;
    
    my $options = GetOptions( "outfile=s" => \$outfile,
                              "images=s" => \$path,);
    
    my @images = sort glob("$path") or die "No Files\n";
    
    my $pdf = PDF::API2::Lite->new();
    for my $png ( sort @images ) {
            my $image = $pdf->image_png( "$png" );
            $pdf->page(1150,450);
            $pdf->image($image, 10, 10);
    }
    
    $pdf->saveas( $outfile );
    
    4 回复  |  直到 16 年前
        1
  •  5
  •   daxim Fayland Lam    16 年前

    等待一天,这样你就可以节省10分钟阅读模块文档。这并不难,太空。

    use PDF::API2 qw();
    
    {
        my $pdf = PDF::API2->open('input.pdf');
    
        for my $index (1 .. $pdf->pages) {
            my $page = $pdf->openpage($index);
            my $txt  = $page->text;
            $txt->textlabel(300, 700, $pdf->corefont('Helvetica Bold'), 12, 'some Header text');
    
            my $gfx = $page->gfx;
            $gfx->image($pdf->image_png('Header_image.png'), 150, 700);
    
            $txt->textlabel(300, 100, $pdf->corefont('Helvetica Bold'), 12, "Page: $index");
        }
    
        $pdf->saveas('output.pdf');
        $pdf->end;
    }
    
        2
  •  4
  •   dwarring    8 年前

    PDF::API2 是我做这类事情的主力。

    我几乎总是使用 importPageIntoForm 方法,只要我需要对现有PDF文档进行任何上架或重新处理。

    作为一个通用解决方案,我一页一页地创建一个新的PDF,导入我想要存储的元素,然后添加额外的文本或图形。

    #!/usr/bin/perl
    use warnings; use strict;
    
    use PDF::API2;
    
    my $infile = shift (@ARGV);
    my $outfile = shift (@ARGV);
    
    die "usage $0: infile outfile"
    unless $infile && $outfile;
    
    my $pdf_in = PDF::API2->open($infile);
    my $pdf_out = PDF::API2->new;
    
    foreach my $pagenum (1 .. $pdf_in->pages) {
    
      my $page_in = $pdf_in->openpage($pagenum);
      #
      # create a new page
      #
      my $page_out = $pdf_out->page(0);
    
      my @mbox = $page_in->get_mediabox;
      $page_out->mediabox(@mbox);
    
      my $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum);
    
      #
      # lay up the input page in the output page
      # note that you can adjust the position and scale, if required
      #
      my $gfx = $page_out->gfx;
    
      $gfx->formimage($xo,
              0, 0, # x y
              1);   # scale
    
      #
      # add page number text
      #
      my $txt = $page_out->text;
    
      $txt->strokecolor('#000000');
    
      $txt->translate(
              my $_x = 200,
              my $_y = 50
      );
    
      my $font = $pdf_out->corefont('Courier');
      $txt->font($font, 12);
      $txt->text( 'Page: '.$pagenum );
    
      #
      # add header image
      #
    
      my $header_img = $pdf_out->image_png('SomeHeader.png');
      $gfx->image($header_img, 0, 400);
    }
    
    $pdf_out->saveas($outfile);
    
        3
  •  2
  •   draegtun    16 年前

    看看 PDF::API2::Simple 相反这个CPAN模块提供了一些方便的辅助方法 PDF::API2 包括页眉和页脚。

    下面是一个简单的工作页眉/页脚示例:

    use 5.012;
    use warnings;
    use PDF::API2::Simple;
    
    our $PageNo;
    
    my $pdf = PDF::API2::Simple->new(
        file   => 'file.pdf',
        header => \&header,
        footer => \&footer,
    );
    
    $pdf->add_font('Verdana');
    
    for my $page (1..3) {
        $pdf->add_page;
        $pdf->image( 'image.png', x => 300, y => 300 );
    }
    $pdf->save;  
    
    
    sub header { shift->text( 'Header text here' ) }
    sub footer { shift->text( 'page:  ' . ++$PageNo, x => 10, y => 10 ) }
    

    /I3az/