11

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()
Community
  • 1
  • 1
Joe McGrath
  • 1,481
  • 10
  • 26

5 Answers5

4

You can try the pypdf library. In 2022 PyPDF2 was merged back into pypdf.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Scott A
  • 7,745
  • 3
  • 33
  • 46
3

With pdfjam, the command to translate all pages 1 inch to the right is

pdfjam --offset '1in 0in' doc.pdf 

The transformed document is saved to doc-pdfjam.pdf. For further options, type pdfjam --help. Currently pdfjam requires a Unix-like command prompt (Linux, Mac, or Cygwin). In Ubuntu, it can be installed with

sudo apt install pdfjam
Ben Mares
  • 1,764
  • 1
  • 15
  • 26
3

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

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

Dingo
  • 2,619
  • 1
  • 22
  • 32
1

Not a full answer, but you can use LaTeX with pdfpages: http://www.ctan.org/tex-archive/macros/latex/contrib/pdfpages/

Multiple commandline linux tools also use this approach, for instance pdfjam uses this: http://www2.warwick.ac.uk/fac/sci/statistics/staff/academic-research/firth/software/pdfjam

Maybe pdfjam can already provide what you need already.

markijbema
  • 3,985
  • 20
  • 32
  • Looks like a lot of dependencies and a large learning curve. An external call to pdfjam looks like the way to go. Thanks for the quick answer. – Joe McGrath Nov 01 '11 at 23:00
  • If you don't know LaTeX it is ;) However, LaTeX is a good tool to have in your toolbox imho. It's great for creating pdfs fast from a text source. And the dependencies don't trouble me as much in linux as the do in windows/macosx. You can just apt-get the lot, and it works. But glad this helped you :) – markijbema Nov 01 '11 at 23:03
0

Here is a modified version for python3.x.

First install pypdf2 via pip install pypdf2

import sys
import os
from PyPDF2 import PdfFileReader, PdfFileWriter

uToShift = 40; # amount to shift contents by. +ve shifts right

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()

path=os.path.dirname(os.path.realpath(__file__))
with open(("%s\\%s" % (path, sys.argv[1])), "rb") as pdfin:
    with open(("%s\\%s" % (path, sys.argv[2])), "wb") as pdfout:
        pdfInput = PdfFileReader(pdfin)
        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 )

        pdfOutput.write(pdfout)
Klik
  • 1,757
  • 1
  • 21
  • 38