Python PyMuPDF detect that the page is rotated 90 degrees in a PDF file

117 views Asked by At

I'm trying to insert text on each page of a PDF file, but if the page is flipped 90 degrees, then the text is inserted also rotated 90 degrees. Рages of different formats. How can I recognize that the page is flipped 90 degrees?

import fitz

if __name__ == '__main__':
    src_pdf_filename = 'original.pdf'
    dst_pdf_filename = 'destination.pdf'

    document = fitz.open(src_pdf_filename)

    ss2 = u"В производство работ"
    ss3 = u"29.11.2023"
    ss4 = u"главный инженер проекта"
    ss5 = u"Бендер О.О."

    font = fitz.Font("tiro")  # this is a full font file

    for page in document:

        page.insert_font(fontname="F0", fontbuffer=font.buffer)

        page.insert_text((page.mediabox.width-250, page.mediabox.height-280), ss2, fontname="F0", fontsize=18,
                         encoding=fitz.TEXT_ENCODING_CYRILLIC, color= (0, 0, 1), overlay=False)
        page.insert_text((page.mediabox.width - 200, page.mediabox.height - 260), ss3, fontname="F0", fontsize=18,
                         encoding=fitz.TEXT_ENCODING_CYRILLIC, color=(0, 0, 1), overlay=False)
        page.insert_text((page.mediabox.width - 250, page.mediabox.height - 240), ss4, fontname="F0", fontsize=18,
                         encoding=fitz.TEXT_ENCODING_CYRILLIC, color=(0, 0, 1), overlay=False)
        page.insert_text((page.mediabox.width - 250, page.mediabox.height - 220), ss5, fontname="F0", fontsize=18,
                         encoding=fitz.TEXT_ENCODING_CYRILLIC, color=(0, 0, 1), overlay=False)

    document.save(dst_pdf_filename)

    document.close()
0

There are 0 answers