#!/usr/bin/perl use File::Basename; use Getopt::Long; use PDF::API2; use strict; # Process command line arguments and populate corresponding variables 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, associate the given page # ranges to be extracted with the last input file name supplied. 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 document # 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 --page argument # associated with the given input file. By default, all pages of 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 extension 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;