【问题标题】:Draw lines using PyPDFDraw lines using PyPDF
【发布时间】:2022-12-26 15:26:45
【问题描述】:

I am currently merging two pages into a single page in PyPDF3 but I need to draw a line in the middle of the two pages. Is this possible? Below is the sample code for reference. Thanks in advance!

from PyPDF3 import PdfFileWriter, PdfFileReader
from PyPDF3.pdf import PageObject

pdf_file = "Plan.pdf"

inputPDF = PdfFileReader(open(pdf_file, "rb"), strict=False)
outputPDF = PdfFileWriter()

for x in range(0, inputPDF.numPages, 2):
    page1 = inputPDF.getPage(x).rotateClockwise(90)
    page2 = inputPDF.getPage(x + 1).rotateClockwise(90)
    
    total_width = max([page1.mediaBox.upperRight[0],page2.mediaBox.upperRight[0]])
    total_height = page1.mediaBox.upperRight[1] + page2.mediaBox.upperRight[1]
    
    new_page = PageObject.createBlankPage(None, total_width, total_height)
    
    new_page.mergeTranslatedPage(page1, 0, page1.mediaBox.upperRight[1])
    new_page.mergeTranslatedPage(page2, 0, 0)    
    
    outputPDF.addPage(new_page.rotateCounterClockwise(90))
    
outputFile = "Merged_Plan.pdf"
outputPDF.write(open(outputFile, "wb"))

【问题讨论】:

    标签: python pdf pypdf


    【解决方案1】:

    You can use the Line annotation: https://pypdf.readthedocs.io/en/latest/user/adding-pdf-annotations.html#line

    It was recently added to pypdf. You might need to update your installed version.

    I recommend to switch away from PyPDF2 / PyPDF3 / PyPDF4 towards pypdf.

    【讨论】:

      猜你喜欢
      • 2022-12-27
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      相关资源
      最近更新 更多