我想将现有pdf文档的所有页面都移动一英寸,这样就可以打三孔而不打内容。将生成pdf文档,因此无法更改生成方式。
似乎iText可以从previous question执行此操作。
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()
答案 0 :(得分:4)
你可以试试pyPdf:
答案 1 :(得分:4)
我可以确认pyPdf真的很好,应该是你问题的解决方案。
答案 2 :(得分:3)
在 Linux
中执行此任务的两种方法使用 ghostscript 通过 gsview
在 / root 或 / home 中查找隐藏文件 .gsview.ini
转到:
[pdfwrite选项]
选项=
X偏移= 0
Y偏移= 0
更改X轴的值,设置一个方便的值(值在postscript点, 1英寸 = 72个后记点)
这样:
[pdfwrite选项]
选项=
X偏移= 72
Y偏移= 0
关闭 .gsview.ini
使用 gsview
file / convert / pdfwrite
现在为偶数页面重复相同的步骤
[pdfwrite选项]
选项=
X偏移= -72
Y偏移= 0
现在你需要将这两个pdf与奇数页和偶数页混合
你可以使用:
Pdf Transformer
java -jar ./pdf-transformer-0.4.0.jar <INPUT_FILE_NAME1> <INPUT_FILE_NAME2> <OUTPUT_FILE_NAME> merge -j
2::使用 podofobox + pdftk
第一步: pdftk 将整个pdf文档分成两个pdf文件,只有奇数和偶数页面
pdftk file.pdf cat 1-endodd output odd.pdf && pdftk file.pdf cat 1-endeven output even.pdf
现在使用 podofobox ,包含在 podofo utils
podofobox file.pdf odd.pdf crop -3600 0 widht height
奇数页和
podofobox file.pdf even.pdf crop 3600 0 widht height
偶数页
宽度和高度在postscript点x 100中,可以在 pdfinfo
中找到例如如果你的pdf文件有pagesize 482x680 ,那么你输入
./podofobox file.pdf odd.pdf crop -3600 0 48200 68000
./podofobox file.pdf even.pdf crop 3600 0 48200 68000
然后你可以将奇数和偶数混合在一个已经引用的唯一文件中
Pdf Transformer
答案 3 :(得分:2)
使用pdfjam
,向右平移1英寸的所有页面的命令是
pdfjam --offset '1in 0in' doc.pdf
已转换的文档已保存到doc-pdfjam.pdf
。有关其他选项,请输入pdfjam --help
。当前pdfjam
需要类似Unix的命令提示符(Linux,Mac或Cygwin)。在Ubuntu中,可以使用
sudo apt install pdfjam
答案 4 :(得分:1)
不是完整的答案,但您可以将LaTeX与pdfpages一起使用: http://www.ctan.org/tex-archive/macros/latex/contrib/pdfpages/
多个命令行linux工具也使用这种方法,例如pdfjam使用这个: http://www2.warwick.ac.uk/fac/sci/statistics/staff/academic-research/firth/software/pdfjam
也许pdfjam已经可以提供你所需要的东西了。
答案 5 :(得分:0)
这是python3.x的修改版本。
首先通过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)