Possible Duplicate:
Split A4 PDF page into two A5 and back again
I have A4 pages in a PDF file like the following:
How could I split each A4 pages to two A5 pages?
Try with:
If this function is not already performed by some existing PDF tool, then here is a high-level brainstorm of how I would approach the problem with CAM::PDF. I'm not sure if it would work.
Off the top of my head I don't know if the extra out-of-cropbox content would be invisible, or if it would affect the render...
UPDATE: I implemented a mostly-working, simplistic solution, as follows. It duplicates the page in question, rotates both copies, and sets the CropBox on each, choosing the left half for the first page and the right half of the second page. You may not need the rotation in your case, not sure.
#!/usr/bin/perl
use strict;
use warnings;
use CAM::PDF;
my $pdffile = 't/sample1.pdf';
my $pdfout = 'temp2.pdf';
my $pagenum = 1;
my $pdf = CAM::PDF->new($pdffile) or die $CAM::PDF::errstr;
my ($objnum, $gennum) = $pdf->getPageObjnum($pagenum);
my $pagedict = $pdf->getPage($pagenum);
$pagedict->{Rotate} = CAM::PDF::Node->new('number', 90);
my $oldbox = $pdf->getValue($pagedict->{CropBox} || $pagedict->{MediaBox});
my @box = map {$pdf->getValue($_)} @{$oldbox};
$pagedict->{CropBox} = CAM::PDF::Node->new('array', [
map {CAM::PDF::Node->new('number', $_)} $box[0], $box[1], $box[2], ($box[3]+$box[1])/2
]);
my $duplicate = CAM::PDF->new($pdffile) or die $CAM::PDF::errstr;
$duplicate->extractPages($pagenum);
$pdf->appendPDF($duplicate); # appends at end instead of inserting
$pagedict = $pdf->getPage($pdf->numPages());
$pagedict->{Rotate} = CAM::PDF::Node->new('number', 90);
$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->cleanoutput($pdfout);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With