裁剪.pdf文件的页面 [英] Cropping pages of a .pdf file
问题描述
我想知道是否有人在以编程方式使用.pdf文件方面有任何经验.我有一个.pdf文件,我需要将每页裁切成一定大小.
I was wondering if anyone had any experience in working programmatically with .pdf files. I have a .pdf file and I need to crop every page down to a certain size.
在Google进行快速搜索之后,我发现了python的pyPdf库,但是我的实验失败了.当我更改页面对象上的cropBox和trimBox属性时,结果不是我所期望的,并且看起来非常随机.
After a quick Google search I found the pyPdf library for python but my experiments with it failed. When I changed the cropBox and trimBox attributes on a page object the results were not what I had expected and appeared to be quite random.
有人对此有任何经验吗?代码示例将不胜感激,最好使用python.
Has anyone had any experience with this? Code examples would be well appreciated, preferably in python.
推荐答案
pypdf在这方面做了我期望的工作.使用以下脚本:
pypdf does what I expect in this area. Using the following script:
#!/usr/bin/python
#
from pyPdf import PdfFileWriter, PdfFileReader
with open("in.pdf", "rb") as in_f:
input1 = PdfFileReader(in_f)
output = PdfFileWriter()
numPages = input1.getNumPages()
print "document has %s pages." % numPages
for i in range(numPages):
page = input1.getPage(i)
print page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y()
page.trimBox.lowerLeft = (25, 25)
page.trimBox.upperRight = (225, 225)
page.cropBox.lowerLeft = (50, 50)
page.cropBox.upperRight = (200, 200)
output.addPage(page)
with open("out.pdf", "wb") as out_f:
output.write(out_f)
生成的文档有一个修剪框,该修剪框为200x200磅,起始于介质盒内的25,25磅. 裁剪框位于修剪框内25个点.
The resulting document has a trim box that is 200x200 points and starts at 25,25 points inside the media box. The crop box is 25 points inside the trim box.
在使用上述代码处理后,以下是我的示例文档在acrobat专业版中的显示方式:
Here is how my sample document looks in acrobat professional after processing with the above code:
此文档在acrobat Reader中加载后将显示为空白.
This document will appear blank when loaded in acrobat reader.
这篇关于裁剪.pdf文件的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!