Debian Codegraph @

scripts/pdfstamp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/perl
2
3 use 5.006;
4 use strict;
5
6 use Text::PDF::File;
7 use Text::PDF::SFont;
8 use Text::PDF::Utils;
9
10 use Getopt::Std;
11 our ($opt_t,$opt_f,$opt_p,$opt_s,$opt_l);
12 getopts('f:l:ps:t:');
13
14 our $VERSION = 0.02; # MJPH 23-JUL-2001 Re-order to stamp on the top
15
16 unless ((defined $ARGV[1] || $opt_p) && -f $ARGV[0])
17 {
18 die <<'EOT';
19 pdfstamp [-f font] [-l locx,locy] [-s size] infile string
20 Adds the given string to the infile .pdf file at the given location, font and
21 size.
22
23 -f font Font name from the standard fonts [Helvetica]
24 -l locx,locy Location in points from bottom left of page [0,0]
25 -p Use the page number as the string
26 -s size Font size to print at [11]
27 -t ttfile TrueType font file to use (instead of -f)
28 EOT
29 }
30
31 require Text::PDF::TTFont if ($opt_t);
32
33 $opt_f = 'Helvetica' unless $opt_f;
34 $opt_s = 11 unless $opt_s;
35 $opt_l =~ s/,\s*/ /o;
36 $opt_l = "0 0" unless $opt_l;
37
38 my ($pdf, $root, $pgs, $fpgins, $spgins, @pglist);
39
40 $pdf = Text::PDF::File->open($ARGV[0], 1);
41 $root = $pdf->{'Root'}->realise;
42 $pgs = $root->{'Pages'}->realise;
43
44 $fpgins = PDFDict(); $pdf->new_obj($fpgins);
45 $spgins = PDFDict(); $pdf->new_obj($spgins);
46 $fpgins->{' stream'} = "q";
47 $spgins->{' stream'} = "Q";
48
49 @pglist = proc_pages($pdf, $pgs);
50
51 my $max = 0;
52 foreach my $p (@pglist)
53 {
54 my $dict = $p->find_prop('Resources');
55 if (defined $dict && defined $dict->{'Font'})
56 {
57 foreach my $k (keys %{$dict->{'Font'}})
58 {
59 next unless $k =~ m/^ap([0-9]+)/o;
60 my $val = $1;
61 $max = $val if $val > $max;
62 }
63 }
64 }
65
66 $max++;
67 my $font;
68 if ($opt_t)
69 { $font = Text::PDF::TTFont->new($pdf, $opt_t, "ap$max", -subset => 1) || die "Can't work with font $opt_t"; }
70 else
71 { $font = Text::PDF::SFont->new($pdf, $opt_f, "ap$max") || die "Can't create font $opt_f"; }
72 my $stream = PDFDict();
73 $stream->{' stream'} = "BT 1 0 0 1 $opt_l Tm /ap$max $opt_s Tf " . $font->out_text($ARGV[1]) . " Tj ET";
74 $pdf->new_obj($stream);
75 my $count = 1;
76 foreach my $p (@pglist)
77 {
78 my ($s) = $stream;
79 if ($opt_p)
80 {
81 $s = PDFDict();
82 $s->{' stream'} = "BT 1 0 0 1 $opt_l Tm /ap$max $opt_s Tf " . $font->out_text($count++) . " Tj ET";
83 $pdf->new_obj($s);
84 }
85 $p->add_font($font, $pdf);
86 $p->{Contents} = PDFArray($fpgins, $p->{Contents}->elementsof, $spgins, $s);
87 $pdf->out_obj($p);
88 }
89
90 $pdf->close_file;
91
92 sub proc_pages
93 {
94 my ($pdf, $pgs) = @_;
95 my ($pg, $pgref, @pglist, $pcount);
96
97 foreach $pgref ($pgs->{'Kids'}->elementsof)
98 {
99 $pg = $pdf->read_obj($pgref);
100 if ($pg->{'Type'}->val =~ m/^Pages$/oi)
101 { push(@pglist, proc_pages($pdf, $pg)); }
102 else
103 {
104 $pgref->{' pnum'} = $pcount++;
105 push (@pglist, $pgref);
106 }
107 }
108 (@pglist);
109 }
110