用PyPDF2纠正方向信息错误的PDF页面

2024-10-03 13:18:59 发布

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

好的。我正在尝试将多个PDF文档合并到一个文档中。然而,这些文件有不同的来源,有些是在计算机中创建的,有些是用不同的扫描仪/软件扫描的。所以,在加入他们之前,我要把他们都缩放到A4大小。你知道吗

我的问题是有些文档显示正常,但当我检查方向时,它看起来好像文档被旋转了。你知道吗

例如,对于这个文档here,它在浏览器和Acrobat Reader中显示OK,但是如果我使用PyPDF2获取信息:

pdf_reader = PdfFileReader(path)
for page in range(pdf_reader.getNumPages()):
    this_page = pdf_reader.getPage(page)
    orientation = this_page.get('/Rotate')
    print(f"Document: {path}")
    print(f"    Orientation: {orientation}")
    print(f"    mediaBox:    {this_page.mediaBox}")
    print(f"    artBox:      {this_page.artBox}")
    print(f"    bleedBox:    {this_page.bleedBox}")
    print(f"    cropBox:     {this_page.cropBox}")
    print(f"    trimBox:     {this_page.trimBox}")

我得到:

        Orientation: 90
        mediaBox:    RectangleObject([0, 0, 792, 542])
        artBox:      RectangleObject([0, 0, 792, 542])
        bleedBox:    RectangleObject([0, 0, 792, 542])
        cropBox:     RectangleObject([0, 0, 792, 542])
        trimBox:     RectangleObject([0, 0, 792, 542])

这很烦人,因为在接下来的步骤中,我将页码添加到文档中,但由于方向的原因,这些页码都放错了位置。你知道吗

请注意,页面显示正确,但不知何故它们只有错误的方向数据。如果我尝试设置旋转页面的方向,例如

this_page.rotateClockwise(-orientation)

然后它们会横向显示。你知道吗

有什么建议可以帮我纠正方向吗?你知道吗

非常感谢!你知道吗


Tags: path文档pdfpagethis方向readerprint