#!/usr/bin/perl
use File::Basename;
use Getopt::Long;
use PDF::API2;
use strict;
# Process command line arguments and populate corresponding variable
+s
my %pages = ();
my @input = ();
GetOptions(
'i|input=s' => \@input,
'o|output=s' => \( my $output = '' ),
'p|page|pages=s' => sub {
if (scalar @input > 0) {
# If an input file name has previously been defined, associa
+te the given page
# ranges to be extracted with the last input file name suppl
+ied.
my @files = split /,/, $input[-1];
push @{ $pages{ $_ } }, $_[1] foreach @files;
}
}
);
exit 1 unless scalar @input > 0 and length $output > 0;
# Split the input files specified on any comma characters present -
+This
# allows for multiple input files to be specified either by multiple
+ --input
# arguments or by a single argument in a comma delimited fashion.
@input = map { split /,/ } @input;
# Open the PDF file for output (via the PDF::API2 object constructor
+)
my $pdf = PDF::API2->new( -file => $output );
my $root = $pdf->outlines;
# Step through each of the input files specified and extract the doc
+ument
# pages with the options specified.
my $import_page = 0;
foreach my $file ( @input ) {
my $input = PDF::API2->open( $file );
# Expand the page list and range definitions passed with the --p
+age argument
# associated with the given input file. By default, all pages o
+f the input
# file are included in the output.
my @pages = ();
if ( exists $pages{ $file } ) {
@pages = map { split /,/ } @{ $pages{ $file } };
@pages = map { /^(\d+)-(\d+)$/ ? $1 .. $2 : $_ } @pages;
}
else {
@pages = 1 .. $input->pages;
}
# Import the pages from the input file input the output PDF file
+ being
# constructed
if (scalar @pages > 0) {
# Extract the filename of the input file without the file ex
+tension for
# incorporation into the document outline.
my ($name, undef, undef) = fileparse($file, '\.[^\.]*');
my $outline = $root->outline;
$outline->title( $name );
# Step through each of the pages to be imported, import the
+page and add an
# entry to the document outline.
my $document_page = 0;
foreach (@pages) {
++$import_page;
++$document_page;
my $page = $pdf->importpage($input, $_, $import_page);
my $bookmark = $outline->outline;
$bookmark->title("Page $document_page");
$bookmark->dest($page);
$outline->dest($page) if $document_page == 1;
}
}
}
$pdf->preferences( -outlines => 1 );
$pdf->update;
$pdf->end;
exit 0;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|