-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_split.pl
More file actions
44 lines (32 loc) · 1.22 KB
/
pdf_split.pl
File metadata and controls
44 lines (32 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl
use strict;
use warnings;
use CAM::PDF;
my $pdffile = shift;
my $pdfout = shift;
my $pdf = CAM::PDF->new($pdffile) or die $CAM::PDF::errstr;
foreach my $pagenum (1..$pdf->numPages) {
# get some values for cropping
my $pagedict = $pdf->getPage(1);
my ($objnum, $gennum) = $pdf->getPageObjnum(1);
my $oldbox = $pdf->getValue($pagedict->{CropBox} || $pagedict->{MediaBox});
my @box = map {$pdf->getValue($_)} @{$oldbox};
# add the page twice and crop it
my $duplicate = CAM::PDF->new($pdffile) or die $CAM::PDF::errstr;
$duplicate->extractPages($pagenum);
$pdf->appendPDF($duplicate);
$pdf->appendPDF($duplicate);
$pagedict = $pdf->getPage($pdf->numPages);
$pagedict->{CropBox} = CAM::PDF::Node->new('array', [
map {CAM::PDF::Node->new('number', $_)} $box[0], $box[1], $box[2], ($box[3]+$box[1])/2
]);
$pagedict = $pdf->getPage($pdf->numPages - 1);
$pagedict->{CropBox} = CAM::PDF::Node->new('array', [
map {CAM::PDF::Node->new('number', $_)} $box[0], ($box[3]+$box[1])/2, $box[2], $box[3]
]);
if ($objnum) {
$pdf->{changes}->{$objnum} = 1;
}
$pdf->deletePage(1);
}
$pdf->cleanoutput($pdfout);