Lennart Steinke
Lennart Steinke

Reputation: 614

Adding extra space to pages in PDF

I have a couple of PDFs I want to add a few inches on one side to give myself more room for handwritten comments in a notes app. Basically, I want to give myself more room to scribble on the sides of the pages (lecture scripts).

The pages should not be scaled, I simply want the contents to stay at the same spot from the upper left corner, but add more space at the right and maybe at the bottom.

Is there a good way to to this either using one of the Python PDF libs or using a command line tool?

Can I simply add extra space to the Media box, or do I need to do something else?

Upvotes: 0

Views: 1212

Answers (1)

Lennart Steinke
Lennart Steinke

Reputation: 614

OK, the following code seems to work. Had to set mediaBox and cropBox to get the desired result.

from PyPDF2 import PdfFileReader, PdfFileWriter

pdf = PdfFileReader("org.pdf")

writer = PdfFileWriter()

factor = 1.3

for page in pdf.pages:
    x,y = page.mediaBox.lowerRight 
    page.mediaBox.lowerRight = ( (factor * float(x)), float(y))

    x,y = page.cropBox.lowerRight 
    page.cropBox.lowerRight = ( (factor * float(x)), float(y))

    writer.addPage( page )

with open("out.pdf", "wb") as out_f:
    writer.write(out_f)

Upvotes: 1

Related Questions