5

ヘッダー(テキストと1つの画像付き)とフッター(ページ番号付き)を画像で追加することは可能ですか. 以下のコードを書いて、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

3 に答える 3

5

SOで1日待つと、モジュールのドキュメントを読むのに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;
}
于 2010-06-02T08:49:32.907 に答える
4

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);
于 2010-06-15T23:28:44.437 に答える
2

PDF::API2::Simple代わりにご覧ください。PDF::API2この CPAN モジュールは、ヘッダーとフッターを含むいくつかの便利なヘルパー メソッドを提供します。

以下は、ヘッダー/フッターの簡単な作業例です。

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/

于 2010-06-06T20:31:41.330 に答える