PySimpleGUI/Demo_PDF_Viewer.py

80 lines
2.1 KiB
Python
Raw Normal View History

2018-08-20 22:43:33 +00:00
import sys
import fitz
import PySimpleGUI as sg
try:
fname = sys.argv[1]
except:
fname = 'C:/Python/PycharmProjects/GooeyGUI/test.pdf'
doc = fitz.open(fname)
title = "PyMuPDF display of '%s' (%i pages)" % (fname, len(doc))
def get_page(pno, zoom = 0):
page = doc[pno]
r = page.rect
mp = r.tl + (r.br - r.tl) * 0.5
mt = r.tl + (r.tr - r.tl) * 0.5
ml = r.tl + (r.bl - r.tl) * 0.5
mr = r.tr + (r.br - r.tr) * 0.5
mb = r.bl + (r.br - r.bl) * 0.5
mat = fitz.Matrix(2, 2)
if zoom == 1:
clip = fitz.Rect(r.tl, mp)
elif zoom == 4:
clip = fitz.Rect(mp, r.br)
elif zoom == 2:
clip = fitz.Rect(mt, mr)
elif zoom == 3:
clip = fitz.Rect(ml, mb)
if zoom == 0:
pix = page.getPixmap(alpha = False)
else:
pix = page.getPixmap(alpha = False, matrix = mat, clip = clip)
return pix.getPNGData()
form = sg.FlexForm(title, return_keyboard_events=True)
data = get_page(0)
image_elem = sg.Image(data=data)
layout = [ [image_elem],
[sg.ReadFormButton('Next'),
sg.ReadFormButton('Prev'),
sg.ReadFormButton('First'),
sg.ReadFormButton('Last'),
sg.ReadFormButton('Zoom-1'),
sg.ReadFormButton('Zoom-2'),
sg.ReadFormButton('Zoom-3'),
sg.ReadFormButton('Zoom-4'),
sg.Quit()] ]
form.Layout(layout)
2018-08-20 23:09:05 +00:00
i = oldzoom = 0
2018-08-20 22:43:33 +00:00
while True:
button,value = form.Read()
zoom = 0
if button in (None, 'Quit'):
break
if button in ("Next", 'Next:34'):
i += 1
elif button in ("Prev", "Prior:33"):
i -= 1
elif button == "First":
i = 0
elif button == "Last":
i = -1
elif button == "Zoom-1":
2018-08-20 23:09:05 +00:00
zoom = oldzoom = 0 if oldzoom == 1 else 1
2018-08-20 22:43:33 +00:00
elif button == "Zoom-2":
2018-08-20 23:09:05 +00:00
zoom = oldzoom = 0 if oldzoom == 2 else 2
2018-08-20 22:43:33 +00:00
elif button == "Zoom-3":
2018-08-20 23:09:05 +00:00
zoom = oldzoom = 0 if oldzoom == 3 else 3
2018-08-20 22:43:33 +00:00
elif button == "Zoom-4":
2018-08-20 23:09:05 +00:00
zoom = oldzoom = 0 if oldzoom == 4 else 4
2018-08-20 22:43:33 +00:00
try:
data = get_page(i, zoom)
except:
i = 0
data = get_page(i, zoom)
image_elem.Update(data=data)