views:

128

answers:

2

Hi all,

I have a PDF document which I need to shift the pages to the right several inches. I.e like putting a margin on the left hand side of the page.

Can either CAM::PDF or PDF::API2 do it? Or is there anyone have experience with it?

Thanks.

+1  A: 

I'm the author of CAM::PDF. The following little program shifts the page contents right by 100 points.

use CAM::PDF;
my $pdf = CAM::PDF->new('my.pdf');
my $page = $pdf->getPage(1);
$page->{MediaBox}->{value}->[0]->{value} -= 100;
$page->{MediaBox}->{value}->[2]->{value} -= 100;
$pdf->cleanoutput('out.pdf');

I used "use Data::Dumper; print Dumper($page);" to remind myself of the $page data structure.

Chris Dolan
A: 

You can do that also with Ghostscript. I'll give you some example commands for Windows (when using Unix, just replace gswin32c.exe by gs):

gswin32c.exe ^
   -o input-shifted-pages-1-inch-to-left.pdf ^
   -sDEVICE=pdfwrite ^
   -c "<</PageOffset [-72 0]>> setpagedevice" ^
   -f /path/to/input.pdf
  1. -o : specifies the output file. Implicitly also uses -dNOPAUSE -dBATCH -dSAFER.
  2. -sDEVICE=... : asks Ghostscript to output PDF.
  3. -c <<... : a PostScript code snippet passed on the commandline to make happen the page shift
  4. -f ... : specifies the input PDF (-f is required after using -c).

The units used by /PageShift are PostScript points. 72 pt == 1 inch. The value [-72 0] shifts 72pt==1in to the left and 0in to top/bottom. Now you know how to shift 2 inches to the right:

gswin32c ^
   -o input-shifted-pages-2-inches-to-right.pdf ^
   -sDEVICE=pdfwrite ^
   -c "<</PageOffset [144 0]>> setpagedevice ^
   -f /path/to/input.pdf

Wanna shift 0.5 in to the bottom and 1 inch to the right?

gswin32c.exe ^
   -o input-shifted-pages-1-inch-to-right-half-inch-down.pdf ^
   -sDEVICE=pdfwrite ^
   -c "<</PageOffset [72 -36]>> setpagedevice ^
   -f /path/to/input.pdf
pipitas