pyPdf如何理解文档边界?

2024-06-28 11:31:28 发布

您现在位置:Python中文网/ 问答频道 /正文

Here我找到了用于拆分pdf页面的代码。在

#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
    q = copy.copy(p)
    (w, h) = p.mediaBox.upperRight
    p.mediaBox.upperRight = (w/2, h)
    q.mediaBox.upperLeft = (w/2, h)
    output.addPage(p)
    output.addPage(q)
output.write(sys.stdout)

如果一个页面包含四个类似这样的页面:

^{pr2}$

然后,代码将拆分为两个页面(按此顺序),其中包含另外两个页面:

+-------+-------+
|   3   |   4   |
+-------+-------+

+-------+-------+
|   1   |   2   |
+-------+-------+

您可以在following文档上测试它。{{Py2>中提到的其他变量,{1}如代码所示:

UL(0,10)        UR(10,10)
+-------+-------+
|   1   |   2   |
|-------+-------|
|   3   |   4   |
+-------+-------+
LL(0,0)         LR(10,0)

UL(x,y) = UpperLeft
UR(x,y) = UpperRight
LL(x,y) = LowerLeft
LR(x,y) = LowerRight

根据上述规范:

(w, h) = p.mediaBox.upperRight
p.mediaBox.upperRight = (w/2, h)
q.mediaBox.upperLeft = (w/2, h)

我期待着这样的结果:

p:
+-------+
|   1   |
|-------+
|   3   |
+-------+

q:
+-------+
|   2   |
|-------+
|   4   |
+-------+

我错过了什么?在


Tags: 代码inimportforinputoutputsys页面
1条回答
网友
1楼 · 发布于 2024-06-28 11:31:28

在PDF中,有两种方法可以获得横向页面:

  1. 定义宽度>高度的页面。在
  2. 定义纵向页面(宽度<;高度)和旋转(90度、270度等)。在

示例PDF使用第二种方式:所有页面都是595x842,旋转270度。不考虑旋转会导致垂直被解释为水平,反之亦然。在

相关问题 更多 >