I am trying to code a program that will take in two files: a question paper and an answer paper. The two files should be rendered in the same window and have a vertical scroll bar that can be used.
Below is my code:
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
from tkinter import *
from PIL import Image, ImageTk
import os
from pdf2image import convert_from_path
class PDFViewerApp:
def __init__(self, root):
self.root = root
self.root.title("PDF Question Cropper")
self.frame = ttk.Frame(self.root)
self.frame.pack(fill="both", expand=True)
self.question_file = None
self.answer_file = None
self.question_label = ttk.Label(self.frame, text="No question file selected.")
self.question_label.pack()
self.select_question_button = ttk.Button(self.frame, text="Select Question PDF", command=self.select_question_pdf)
self.select_question_button.pack()
self.answer_label = ttk.Label(self.frame, text="No answer file selected.")
self.answer_label.pack()
self.select_answer_button = ttk.Button(self.frame, text="Select Answer PDF", command=self.select_answer_pdf)
self.select_answer_button.pack()
self.confirm_button = ttk.Button(self.frame, text="Confirm and Process", command=self.process_pdfs)
self.confirm_button.pack()
self.new_window = None
self.pdf_canvas1 = None
self.pdf_canvas2 = None
def select_question_pdf(self):
self.question_file = filedialog.askopenfilename(title="Select Question PDF")
if self.question_file:
self.question_label.config(text=f"Question PDF: {os.path.basename(self.question_file)}")
def select_answer_pdf(self):
self.answer_file = filedialog.askopenfilename(title="Select Answer PDF")
if self.answer_file:
self.answer_label.config(text=f"Answer PDF: {os.path.basename(self.answer_file)}")
def process_pdfs(self):
if not self.question_file or not self.answer_file:
return
self.open_new_window()
def open_new_window(self):
self.new_window = tk.Toplevel(self.root)
self.new_window.title("Question Cropper")
self.pdf_viewer_frame = ttk.Frame(self.new_window)
self.pdf_viewer_frame.pack(fill=tk.BOTH, expand=True)
self.pdf_canvas1 = tk.Canvas(self.pdf_viewer_frame, xscrollincrement=1)
self.pdf_canvas2 = tk.Canvas(self.pdf_viewer_frame, xscrollincrement=1)
self.scrollbar1 = ttk.Scrollbar(self.pdf_viewer_frame, orient="vertical", command=self.pdf_canvas1.yview)
self.scrollbar2 = ttk.Scrollbar(self.pdf_viewer_frame, orient="vertical", command=self.pdf_canvas2.yview)
self.scrollbar1.pack(side="right", fill="y")
self.scrollbar2.pack(side="right", fill="y")
self.pdf_canvas1.config(yscrollcommand=self.scrollbar1.set)
self.pdf_canvas2.config(yscrollcommand=self.scrollbar2.set)
self.pdf_canvas1.pack(side="left", fill="both", expand=True)
self.pdf_canvas2.pack(side="right", fill="both", expand=True)
self.file_label = ttk.Label(self.new_window, text="Processing...")
self.file_label.pack()
self.display_pdfs()
def display_pdfs(self):
if self.question_file and self.answer_file:
question_pages = convert_from_path(self.question_file)
answer_pages = convert_from_path(self.answer_file)
if question_pages and answer_pages:
max_width = max(page.width for page in question_pages + answer_pages)
max_height = max(page.height for page in question_pages + answer_pages)
for page in range(min(len(question_pages), len(answer_pages))):
img1 = question_pages[page]
img2 = answer_pages[page]
img1_blank = Image.new('RGB', (max_width, max_height), (255, 255, 255))
img2_blank = Image.new('RGB', (max_width, max_height), (255, 255, 255))
img1_blank.paste(img1, (0, 0))
img2_blank.paste(img2, (0, 0))
photo1 = ImageTk.PhotoImage(master=self.new_window, image=img1_blank)
photo2 = ImageTk.PhotoImage(master=self.new_window, image=img2_blank)
canvas_width = max_width
canvas_height = max_height
self.pdf_canvas1.config(scrollregion=(0, 0, canvas_width, canvas_height), width=canvas_width, height=canvas_height)
self.pdf_canvas2.config(scrollregion=(0, 0, canvas_width, canvas_height), width=canvas_width, height=canvas_height)
self.pdf_canvas1.create_image(0, 0, image=photo1, anchor=tk.NW)
self.pdf_canvas2.create_image(0, 0, image=photo2, anchor=tk.NW)
self.pdf_canvas1.photo = photo1
self.pdf_canvas2.photo = photo2
self.file_label.config(text=f"Question File: {os.path.basename(self.question_file)}\nAnswer File: {os.path.basename(self.answer_file)} - Page {page + 1}")
if __name__ == "__main__":
root = tk.Tk()
app = PDFViewerApp(root)
root.mainloop()
It will be great if anyone can help.
Currently, I am facing three main issues:
-
The two files are not taking up the same amount of space in the window; the question paper, which is placed on the left, appears larger.
-
The two scroll bars are squeezed to the right and not positioned at the relative right side of each displayed file.
-
The two files cannot be fully rendered. In my testing with the current files, only the third page seems to be displayed.
source https://stackoverflow.com/questions/77353994/issues-faced-when-trying-to-render-two-pdf-files
Comments
Post a Comment