裁剪 .pdf 文件的页面 [英] Cropping pages of a .pdf file

查看:25
本文介绍了裁剪 .pdf 文件的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人有任何以编程方式处理 .pdf 文件的经验.我有一个 .pdf 文件,我需要将每一页裁剪成特定大小.

在谷歌快速搜索后,我找到了 python 的 pyPdf 库,但我的实验失败了.当我更改页面对象上的cropBox 和trimBox 属性时,结果与我预期的不同,而且看起来很随机.

有没有人有这方面的经验?代码示例将不胜感激,最好是在 python 中.

解决方案

此文档在 Acrobat Reader 中加载时将显示为空白.

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.

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.

Has anyone had any experience with this? Code examples would be well appreciated, preferably in python.

解决方案

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)

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.

Here is how my sample document looks in acrobat professional after processing with the above code:

This document will appear blank when loaded in acrobat reader.

这篇关于裁剪 .pdf 文件的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆