如何将PDF文档的所有页面移动一英寸?

Joe*_*ath 8 c++ python linux pdf pypdf

我想将现有pdf文档的所有页面都移动一英寸,这样它们就可以打三孔而不会打到内容.将生成pdf文档,因此无法更改生成方式.

似乎iText可以从之前的问题中做到这一点.

什么是C++或Python的等效库(或者这样做)?

如果它是依赖于平台的,我需要一个适用于Linux的工具.

更新:想我会发布一个我编写的小脚本,以防万一其他人找到这个页面并需要它.

工作代码归功于Scott Anderson的建议:

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()
Run Code Online (Sandbox Code Playgroud)

Sco*_*t A 4

您可以尝试pypdf库。2022 年,PyPDF2 被合并回 pypdf。