Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

i want to add my logo to every pdf file in the following way:

1. shift the page 2cm down. 2. add the logo in the new place i've created.

My problem: i have to change the $x and $y for every pdf and i want it to be global for every pdf.

use PDF::API2 qw(); { my $pdf = PDF::API2->open('AAnew.pdf'); for my $index (1 .. $pdf->pages) { my $page = $pdf->openpage($index); $page->mediabox('A4'); #my $txt = $page->text; #$txt->textlabel(300, 700, $pdf->corefont('Helvetica Bold'), 1 +2, 'some Header text'); my $gfx = $page->gfx; $gfx->image($pdf->image_png('aa.png'), 0,0,150,50); $gfx->close; #$txt->textlabel(300, 100, $pdf->corefont('Helvetica Bold'), 1 +2, "Page: $index"); } $pdf->saveas('outputIT.pdf'); $pdf->end; }

i want to add the logo to the SAME posisiton for every pdf file. thanks.

Replies are listed 'Best First'.
Re: PDF API2
by thanos1983 (Parson) on May 04, 2017 at 11:54 UTC
Re: PDF API2
by vr (Curate) on May 04, 2017 at 13:54 UTC
    use strict; use warnings; use PDF::API2; sub _p ($) { $_[ 0 ] / 25.4 * 72 } # mm to postscript points my $pdf = PDF::API2-> open( 'docu.pdf' ); my $logo = $pdf-> image_png( 'logo.png' ); for my $i ( 1 .. $pdf-> pages ) { my $p = $pdf-> openpage( $i ); my ( $llx, $lly, $urx, $ury ) = $p-> get_mediabox; my $gfx = $p-> gfx( 1 ); # prepend content $gfx-> save; $gfx-> translate( 0, _p -20 ); # shift 20 mm down $gfx = $p-> gfx; # append content $gfx-> restore; my $scale = 0.25; # scale 0 .. 1, adjust as desired my $x = # let's make it centered! ( $urx - $llx - $scale * $logo-> width ) / 2; my $y = $ury - _p 20; # 20 mm from top $gfx-> image( $logo, $x, $y, $scale ); } $pdf-> update; # you had a backup, right?