-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_viewer.py
More file actions
76 lines (60 loc) · 2.17 KB
/
pdf_viewer.py
File metadata and controls
76 lines (60 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import fitz # PyMuPDF
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageTk
# Function to open and display a PDF
def open_pdf():
# Get the file path of the PDF to open
filepath = filedialog.askopenfilename(filetypes=[("PDF files", "*.pdf")])
if not filepath:
messagebox.showerror("Input Error", "Please select a PDF file.")
return
try:
# Open the PDF
pdf_document = fitz.open(filepath)
render_page(pdf_document, 0) # Render the first page initially
except Exception as e:
messagebox.showerror("Error", f"Failed to open PDF: {e}")
# Function to render a specific page of the PDF
def render_page(pdf_document, page_number):
if 0 <= page_number < pdf_document.page_count:
page = pdf_document.load_page(page_number) # Load the page
pix = page.get_pixmap() # Render the page to an image
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
img_tk = ImageTk.PhotoImage(img)
img_label.config(image=img_tk)
img_label.image = img_tk
global current_page
current_page = page_number
else:
messagebox.showinfo("Navigation", "No more pages.")
# Function to go to the next page
def next_page():
if pdf_document and current_page < pdf_document.page_count - 1:
render_page(pdf_document, current_page + 1)
# Function to go to the previous page
def previous_page():
if pdf_document and current_page > 0:
render_page(pdf_document, current_page - 1)
# Setting up the GUI
root = tk.Tk()
root.title("PDF Viewer")
root.geometry("600x700")
# PDF image display area
img_label = tk.Label(root)
img_label.pack()
# Navigation buttons
btn_frame = tk.Frame(root)
btn_frame.pack(pady=10)
prev_btn = tk.Button(btn_frame, text="Previous", command=previous_page)
prev_btn.grid(row=0, column=0, padx=10)
next_btn = tk.Button(btn_frame, text="Next", command=next_page)
next_btn.grid(row=0, column=1, padx=10)
# Open PDF button
open_btn = tk.Button(root, text="Open PDF", command=open_pdf)
open_btn.pack(pady=20)
# Global variables
pdf_document = None
current_page = 0
# Run the GUI event loop
root.mainloop()