Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you shift all pages of a PDF document right by one inch?

I want to shift all the pages of an existing pdf document right one inch so they can be three hole punched without hitting the content. The pdf documents will be already generated so changing the way they are generated is not possible.

It appears iText can do this from a previous question.

What is an equivalent library (or way do this) for C++ or Python?

If it is platform dependent I need one that would work on Linux.

Update: Figured I would post a little script I wrote to do this in case anyone else finds this page and needs it.

Working code thanks to Scott Anderson's suggestion:

rightshift.py

#!/usr/bin/python2
import sys
import os
from  pyPdf import PdfFileReader, PdfFileWriter

#not sure what default user space units are. 
# just guessed until current document i was looking at worked
uToShift = 50;

if (len(sys.argv) < 3):
  print "Usage rightshift [in_file] [out_file]"
  sys.exit()

if not os.path.exists(sys.argv[1]):
  print "%s does not exist." % sys.argv[1]
  sys.exit()

pdfInput = PdfFileReader(file( sys.argv[1], "rb"))
pdfOutput = PdfFileWriter()

pages=pdfInput.getNumPages()

for i in range(0,pages):
  p = pdfInput.getPage(i)
  for box in (p.mediaBox, p.cropBox, p.bleedBox, p.trimBox, p.artBox):
    box.lowerLeft = (box.getLowerLeft_x() - uToShift, box.getLowerLeft_y())
    box.upperRight = (box.getUpperRight_x() - uToShift, box.getUpperRight_y())
  pdfOutput.addPage( p )

outputStream = file(sys.argv[2], "wb")
pdfOutput.write(outputStream)
outputStream.close()
like image 497
Joe McGrath Avatar asked Nov 01 '11 22:11

Joe McGrath


People also ask

How do I align all pages in a PDF?

With the PDF containing the content that you want to align/shift open in Acrobat, select "Plug-Ins > AutoPagex Plug-in > Shift and Align Page Content...". Choose an option using the "Align page content:" drop-down menu.

How do you change a PDF so all the pages are the same size?

In Acrobat, you can go to Tools> Print Production> Preflight> (select the wrench icon for changes)> Pages> Set Crop Box to Trim Box. You can also place all of the pages into InDesign and export to a new PDF at the desired size.

How do I make all the pages in Adobe the same size?

You can do this with Acrobat Pro. Go to Tools> Print Production> Prefilght> FixUps (blue wrench icon)> Pages. Go to the little fly-out menu and duplicate the prefilght, give it a new name. Click on Edit, to change the desired final trim size and the method used to get to this size (scale, white border, etc.)


2 Answers

You can try pyPdf:

http://pybrary.net/pyPdf/

like image 184
Scott A Avatar answered Nov 15 '22 21:11

Scott A


two ways to perform this task in Linux

  1. using ghostscript trough gsview

    • look in your /root or /home for the hidden file .gsview.ini

    • go to section:

    [pdfwrite Options]

    Options=

    Xoffset=0

    Yoffset=0

change the values for X axis, settling a convenient value (values are in postscript points, 1 inch = 72 postscript points)

so:

[pdfwrite Options]

Options=

Xoffset=72

Yoffset=0

  • close .gsview.ini

  • open your pdf file with gsview

  • file / convert / pdfwrite

  • select first odd pages and print to a new file (you can name this as odd.pdf)

now repeat same steps for even pages

  • open your pdf file with gsview

[pdfwrite Options]

Options=

Xoffset=-72

Yoffset=0

  • file / convert / pdfwrite
  • select first even pages and print to a new file (you can name this as even.pdf)

now you need to mix these two pdf with odd and even pages

you can use:

Pdf Transformer

  • http://sourceforge.net/projects/pdf-transformer/

java -jar ./pdf-transformer-0.4.0.jar <INPUT_FILE_NAME1> <INPUT_FILE_NAME2> <OUTPUT_FILE_NAME> merge -j


2: : use podofobox + pdftk

  • first step: with pdftk separate whole pdf document in two pdf files with only odd and even pages

    pdftk file.pdf cat 1-endodd output odd.pdf && pdftk file.pdf cat 1-endeven output even.pdf

  • now with podofobox, included into podofo utils

  • http://podofo.sourceforge.net/about.html

  • podofobox file.pdf odd.pdf crop -3600 0 widht height for odd pages and

  • podofobox file.pdf even.pdf crop 3600 0 widht height for even pages

width and height are in postscript point x 100 and can be found with pdfinfo

e.g. if your pdf file has pagesize 482x680, then you enter

./podofobox file.pdf odd.pdf crop -3600 0 48200 68000

./podofobox file.pdf even.pdf crop 3600 0 48200 68000

then you can mix together odd and even in a unique file with already cited

Pdf Transformer

  • http://sourceforge.net/projects/pdf-transformer/
like image 23
Dingo Avatar answered Nov 15 '22 19:11

Dingo