-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpypdfgui.py
More file actions
1663 lines (1556 loc) · 73.1 KB
/
pypdfgui.py
File metadata and controls
1663 lines (1556 loc) · 73.1 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Author: lefkovitj (https://lefkovitzj.com)
File Last Modified: 8/28/2025
Project Name: PyPdfApp
File Name: pypdfgui.py
"""
# Python Standard Library Imports.
import json
import math
import os
import subprocess
import sys
from tkinter import *
from tkinter import ttk
import warnings
import webbrowser
import threading
# Third-party Module Imports.
import customtkinter as ctk
import pymupdf
import PIL
import requests
# Project Imports.
from gui import GuiMenu
from load import open_pdf
from manipulate import (
create_blank_pdf,
gui_get_file,
PageDeletePDF,
PageInsertBlankPDF,
PageMovePDF,
PageRotatePDF,
WatermarkPDF,
PdfExtractor,
PdfMerger
)
from save import save_pdf
from sign import gen_signature_keys, sign_pdf, verify_pdf_signature
from utils import PdfDocInstance, PdfQueue
def on_enter(_event, canvas, rect, set_color):
"""Update color of canvas rectangle on mouse hover enter. """
canvas.itemconfig(rect, outline=set_color)
canvas.update()
def on_leave(_event, canvas, rect, set_color):
"""Update color of canvas rectangle on mouse hover exit. """
canvas.itemconfig(rect, outline=set_color)
canvas.update()
class App():
""" The primary GUI used by PyPdfApp """
# APPLICATION STARTUP
def __init__(self):
"""Internal Application Data & Settings """
self.pdfs = PdfQueue()
self.pdf_id = None
self.file_selected = ""
self.signer_private_key_path = None
self.signer = None
self.scale = 1.0
self.link_images = [] # Must be kept otherwise the link icon flashes in and out.
self.thread = None
# Define attributes for later initialization.
self.tkimgs = None
self.save_path = None
self.tkimg = None
self.redact_toggle = None
self.freehand_start_bind = None
self.freehand_end_bind = None
self.redact_start_bind = None
self.redact_end_bind = None
self.highlight_toggle = None
self.highlight_start_bind = None
self.highlight_end_bind = None
self.active_highlight_start = None
# Get the settings file data.
with open("settings.json", "r", encoding="utf-8") as json_settings:
self.settings = json.load(json_settings)
# Startup checks.
self.needs_update = self.on_startup_update_check()
self.license_agreed = self.on_startup_license_check()
"""Application GUI - Layout & Contents """
# Create the GUI window.
self.root = ctk.CTk(fg_color="#333333")
ctk.set_default_color_theme("blue")
ctk.set_appearance_mode("Dark")
# Add the menu areas.
self.menu_top = ctk.CTkFrame(self.root)
self.menu_top.pack(anchor="nw", fill="x")
self.menu_middle = ctk.CTkFrame(self.root)
self.menu_middle.pack(anchor="nw", fill="x")
self.menu = ctk.CTkFrame(self.menu_top)
self.menu.pack(anchor="nw", fill="x")
self.submenu = ctk.CTkFrame(self.menu_middle)
self.submenu.pack(anchor="nw", fill="x", expand=True)
for i in range(11): # Configure the resizeable grid columns.
self.menu.columnconfigure(i, weight=0)
self.submenu.columnconfigure(i, weight=1)
# Add the top menu and its contents.
self.mode = ctk.CTkOptionMenu(
self.menu,
values=[
"File",
"Pages",
"Encrypt & Compress",
"Insert",
"Extract",
"Meta Data",
"Signatures",
"Markup"],
command=self.set_menu,
width=175)
self.mode.grid(row=0, column=0, columnspan=2, padx=5)
self.prev = ctk.CTkButton(self.menu, text = "<", command = self.previous_page, width = 25)
self.next = ctk.CTkButton(self.menu, text = ">", command = self.next_page, width = 25)
self.prev.grid(row = 0, column = 2, padx=5)
self.next.grid(row = 0, column = 3, padx=5)
# Add the middle menu content (buttons).
self.menu_button_1 = ctk.CTkButton(self.submenu, text=" ", command = lambda: print(""))
self.menu_button_1.grid(row = 0, column=0, columnspan=3, padx=5, pady=10, sticky="nsew")
self.menu_button_2 = ctk.CTkButton(self.submenu, text=" ", command = lambda: print(""))
self.menu_button_2.grid(row = 0,column=3, columnspan=3, padx=5, pady=10, sticky="nsew")
self.menu_button_3 = ctk.CTkButton(self.submenu, text=" ", command = lambda: print(""))
self.menu_button_3.grid(row = 0,column=6, columnspan=3, padx=5, pady=10, sticky="nsew")
self.menu_button_4 = ctk.CTkButton(self.submenu, text=" ", command = lambda: print(""))
self.menu_button_4.grid(row = 0,column=9, columnspan=3, padx=5, pady=10, sticky="nsew")
# Add the Edit Settings area.
self.settings_frame = ctk.CTkFrame(self.menu_middle, height=25)
#self.settings_frame.pack(anchor="nw", fill='x', expand=True)
# Open bottom menu (file selector).
self.file_menu = ctk.CTkFrame(self.menu_middle)
self.file_menu.pack(anchor="ne", fill="x", expand=True)
for i in range(11): # Configure the resizeable grid columns.
self.file_menu.columnconfigure(i, weight=1)
self.file_select_bar = ctk.CTkSegmentedButton(
self.file_menu,
command=self.file_selector_callback,
width=self.file_menu.winfo_width())
self.file_select_bar.grid(
row=0,
column=0,
padx=(0, 0),
pady=(10, 10),
columnspan=12,
sticky="ew")
self.file_select_bar.configure(values=self.pdfs.get_keys())
self.root.update()
self.mode.configure(width=self.root.winfo_width()/4 - 10)
# Add the Page Quickselect area.
self.page_quickset_frame = ctk.CTkFrame(self.root, width=150, fg_color="#333333")
# Zoom controls.
self.scale_frame = ctk.CTkFrame(self.page_quickset_frame, fg_color = "transparent")
self.scale_frame.pack()
self.zoom_minus = ctk.CTkButton(
self.scale_frame,
text = "-",
command = self.scale_down,
width = 25)
self.zoom_minus.grid(row=0, column = 0, padx=5)
self.scale_display = ctk.CTkLabel(
self.scale_frame,
text = "Zoom: 100%",
fg_color = "transparent")
self.scale_display.grid(row=0, column=1, padx=5)
self.zoom_plus = ctk.CTkButton(
self.scale_frame,
text = "+",
command = self.scale_up,
width = 25)
self.zoom_plus.grid(row=0, column = 2, padx=5)
separator = ttk.Separator(self.scale_frame, orient='horizontal')
separator.grid(sticky="ew", columnspan=3, pady = 25)
# Page Quickselect Images.
self.page_count = ctk.CTkLabel(
self.page_quickset_frame,
text = "page x/x",
fg_color = "transparent")
if self.pdfs.is_empty():
self.page_count.configure(text="Page: 0/0")
else:
self.page_count.configure(
text=(
f"Page: {self.pdfs[self.pdf_id].page_i+1}"
f"/{len(self.pdfs[self.pdf_id].doc)}")
)
self.page_count.pack()
self.page_quickset_frame.pack(
anchor="center",
fill='both',
expand=False,
side="left",
padx=(0,20))
self.quickset_canvas = Canvas(self.page_quickset_frame, bg="#333333", highlightthickness=0)
self.quickset_canvas.pack(side="left", anchor='center', fill='both', expand=True)
self.quickset_scrollbar = ctk.CTkScrollbar(
self.page_quickset_frame,
orientation="vertical",
fg_color="#333333")
self.quickset_scrollbar.pack(side="right", fill="y")
self.quickset_scrollbar.configure(command=self.quickset_canvas.yview)
self.quickset_canvas.configure(yscrollcommand=self.quickset_scrollbar.set)
# Add the PDF render area, with vertical and horizontal scrollbars and the canvas.
self.canvas_frame = ctk.CTkFrame(self.root, width=290)
self.canvas_frame.pack(anchor="center", fill='both', expand=True, side="left")
self.pdf_canvas = Canvas(self.canvas_frame, bg="#333333", highlightthickness=0)
self.canvas_frame.bind(
"<Configure>",
lambda e: self.pdf_canvas.configure(scrollregion=self.pdf_canvas.bbox("all")))
self.scrollbar = ctk.CTkScrollbar(
self.canvas_frame,
orientation="vertical",
fg_color="#333333")
self.scrollbar.pack(side="right", fill="y")
self.scrollbar.configure(command=self.pdf_canvas.yview)
self.h_scrollbar = ctk.CTkScrollbar(
self.canvas_frame,
orientation="horizontal",
fg_color="#333333")
self.h_scrollbar.pack(side="bottom", fill="x")
self.h_scrollbar.configure(command=self.pdf_canvas.xview)
self.pdf_canvas.configure(
yscrollcommand=self.scrollbar.set,
xscrollcommand=self.h_scrollbar.set)
self.pdf_canvas.pack(side="left", anchor='center', fill='both', expand=True)
# Startup Checks - Update & License.
# Application update screen.
if self.needs_update:
self.update_area = ctk.CTkFrame(self.root)
self.update_area.place(anchor="nw", relheight=1.0, relwidth=1.0)
ctk.CTkLabel(
self.update_area,
text = "Your software is not up to date. A new version is available!",
fg_color = "transparent"
).pack()
textbox = ctk.CTkTextbox(self.update_area)
textbox.insert(
0.0,
("Your software is no longer up to date. Software updates "
"are strongly encouraged. \nWhile this version should "
"continue to operate, you will potentially be missing "
"out on:\n 1. New application features.\n 2. Security "
"updates.\n 3. Critical bug fixes.\n 4. Improved "
"application efficiency.\n\nBy continuing to use this "
"version of PyPdfApp you may experience some of the "
"following issues:\n 1. Unexpected program crashes.\n "
"2. User interface glitches.\n 3. Functionality failures.\n "
" 4. Possible corruption of PDF files. \n\n To ensure "
"you are using the most recent version of PyPdfApp, select "
"'Open browser for update'. \nFrom there, you can download "
"the newest version of PyPdfApp avaialable! "))
textbox.configure(state="disabled")
textbox.pack(anchor="nw", fill="both", expand=True)
ctk.CTkButton(
self.update_area,
text="Open browser for update.",
command = self.update_web_event
).pack(side="right")
ctk.CTkButton(
self.update_area,
text="Continue without update.",
command = self.update_skip_event
).pack(side="left")
# Application license agreement screen.
if not self.license_agreed:
self.license_area = ctk.CTkFrame(self.root)
self.license_area.place(anchor="nw", relheight=1.0, relwidth=1.0)
ctk.CTkLabel(
self.license_area,
text = "You must agree to the MIT license to continue!",
fg_color = "transparent"
).pack(anchor="nw", fill="x")
with open("license.txt", "r", encoding="utf-8") as license_doc:
license_text = license_doc.read()
textbox = ctk.CTkTextbox(self.license_area)
textbox.insert(0.0, license_text)
textbox.configure(state="disabled")
textbox.pack(anchor="nw", fill="both", expand=True)
ctk.CTkButton(
self.license_area,
text="I agree to the MIT License.",
command = self.license_agree_event
).pack(anchor="nw", fill="x", side="right")
# Initial Configuration - Window, Binds, and Graphics.
# Set window title.
self.root.title("PyPdfApp")
# Set window close functionality.
if bool(self.settings["ask_save_before_exit"]):
# Add on-exit "Are you sure?" event.
self.root.protocol("WM_DELETE_WINDOW", self.app_exit_event)
# Binds.
self.enable_all_keybinds()
self.root.bind("<Control-o>", self.open_new_pdf)
self.root.bind("<Control-n>", self.open_blank_pdf)
# Load the menu data.
self.menus = {
"File": GuiMenu(
"File",
["New", "Open", "Save", "Close"],
[self.open_blank_pdf, self.open_new_pdf, self.save_event, self.close_current_pdf],
[True, True, False, False]),
"Encrypt & Compress": GuiMenu(
"Encryption",
["Set Encryption", "Remove Encryption", "Compress", "Compress (max)"],
[
self.event_set_encryption,
self.event_remove_encryption,
self.event_compress,
self.event_compress_max
],
[True, True, True, True]),
"Pages": GuiMenu(
"Pages",
["Move up", "Move down", "Rotate right", "Rotate left"],
[
self.event_move_up,
self.event_move_down,
self.event_rotate_right,
self.event_rotate_left
],
[True, True, True, True]),
"Meta Data": GuiMenu(
"Meta Data",
["Set Author", "Set Title", "Set Subject", "Add Keywords"],
[
self.event_set_meta_author,
self.event_set_meta_title,
self.event_set_meta_subject,
self.event_set_meta_keywords
],
[True, True, True, True]),
"Insert": GuiMenu(
"Insert",
["Insert PDF", "Insert Blank", "Watermark page", "Watermark document"],
[
self.event_insert_pdf,
self.event_insert_page,
self.event_watermark_page,
self.event_watermark_document
],
[True, True, True, True]),
"Extract": GuiMenu(
"Extract",
["Delete page", "Extract text", "Extract images", "Screenshot Page"],
[
self.event_delete,
self.event_extract_text,
self.event_extract_images,
self.event_screenshot_page
],
[True, True, True, True]),
"Signatures": GuiMenu(
"Signatures",
["Sign PDF", "Verfiy Signature", "Add Signer Account", "Select Signer Account"],
[
self.event_sign_pdf,
self.event_verify_signature,
self.event_add_signer_account,
self.event_select_signer_account
],
[False, True, True, True]),
"Markup": GuiMenu(
"Markup",
["Freehand Draw", "Link Editor", "Redact", "Highlight"],
[
self.event_toggle_freehand_draw,
self.event_toggle_link_editor,
self.event_toggle_redact,
self.event_toggle_highlight
],
[True, True, True, True]),
}
# Set initial menu value.
self.menu = None
self.mode.set("File")
self.set_menu("File")
self.freehand_draw_toggle = False
self.link_editor_toggle = False
self.redact_toggle = False
self.active_redact_start = (None, None)
self.highlight_toggle = False
# Render the starting screen contents.
if not self.pdfs.is_empty(): # Load the first page if a document is specified.
self.update_page(0)
else: # No document open, disable all actions that require an open PDF.
self.disable_all_buttons()
self.disable_all_keybinds()
self.root.update()
# Set window minimum dimensions.
self.root.minsize(int(self.root.geometry().split("x")[0]), 250)
self.root.update()
# Start the main application loop.
self.root.mainloop()
# ASSORTED APPLICATION FUNCTIONALITY
# Buttons & Keybinds
def disable_all_buttons(self, *_args):
"""Disable all buttons related to manipulating a PDF. Used when no PDF is open"""
if self.mode.get() != "File":
self.menu = self.menus["File"]
self.menu_button_1.configure(state="normal")
self.menu_button_2.configure(state="normal")
self.menu_button_3.configure(state="disabled")
self.menu_button_4.configure(state="disabled")
self.next.configure(state="disabled")
self.prev.configure(state="disabled")
self.zoom_plus.configure(state="disabled")
self.zoom_minus.configure(state="disabled")
self.mode.configure(state="disabled")
def enable_all_buttons(self, *_args):
"""Disable all buttons related to manipulating a PDF. Used when no PDF is open"""
self.menu_button_1.configure(state="normal")
self.menu_button_2.configure(state="normal")
self.menu_button_3.configure(state="normal")
self.menu_button_4.configure(state="normal")
self.next.configure(state="normal")
self.prev.configure(state="normal")
self.zoom_plus.configure(state="normal")
self.zoom_minus.configure(state="normal")
self.mode.configure(state="normal")
def disable_all_keybinds(self, *_args):
"""Disable all keybinds that require an open PDF. Used when no PDF is open"""
if bool(self.settings["allow_keyboard_events"]):
self.root.unbind("<Control-s", self.bind_save)
self.root.unbind("<Left>", self.bind_prev_page)
self.root.unbind("<Right>", self.bind_next_page)
self.root.unbind("Control-Key-plus>", self.bind_zoom_out)
self.root.unbind("Control-Key-minus>", self.bind_zoom_in)
self.root.unbind("<Control-w>",self.bind_close)
self.pdf_canvas.unbind("<B1-Motion>")
self.pdf_canvas.unbind("<ButtonRelease-1>")
self.quickset_canvas.unbind("<Button-1>")
self.quickset_canvas.unbind("<MouseWheel>")
def enable_all_keybinds(self, *_args):
"""Enable all keybinds that require an open PDF. Used when the first PDF is (re)opened"""
if bool(self.settings["allow_keyboard_events"]):
self.bind_save = self.root.bind("<Control-s>", self.save_event)
self.bind_prev_page = self.root.bind("<Left>", self.previous_page)
self.bind_next_page = self.root.bind("<Right>", self.next_page)
self.bind_zoom_out = self.root.bind("<Control-Key-plus>", self.scale_up)
self.bind_zoom_in = self.root.bind("<Control-Key-minus>", self.scale_down)
self.bind_close = self.root.bind("<Control-w>", self.close_current_pdf)
self.freehand_start_bind = self.pdf_canvas.bind(
"<B1-Motion>",
self.freehand_mouse_add_coords)
self.freehand_end_bind = self.pdf_canvas.bind(
"<ButtonRelease-1>",
self.freehand_mouse_set_end)
self.quickset_canvas.bind("<Button-1>", self.quickset_canvas_clicked)
self.quickset_canvas.bind("<MouseWheel>", self.quickset_on_mousewheel)
def link_edit_popup(self, _event, rect, initial_url):
"""Create a popup window with contents defined by the args"""
popup = ctk.CTkToplevel(self.root)
popup.title("PyPdfApp")
# Add content to the popup
popup_title_label = ctk.CTkLabel(popup, text="")
popup_title_label.pack(padx=20, pady=20)
url_input = ctk.CTkEntry(popup)
url_input.insert(0, initial_url)
url_input.pack()
label = ctk.CTkLabel(popup, text="")
label.pack(padx=20, pady=20)
popup.grab_set()
save_button = ctk.CTkButton(
popup,
text="Update Link",
command=lambda: self.process_link_update(rect, popup, url_input))
save_button.pack(pady=0, side="right")
close_button = ctk.CTkButton(popup, text="Cancel Changes", command=popup.destroy)
close_button.pack(pady=0, side="left")
def on_click(self, _event, rect):
"""Handle click of URL"""
mydoc = self.pdfs[self.pdf_id].doc
mypage = self.pdfs[self.pdf_id].page_i
for page_i, page in enumerate(mydoc):
if page_i == mypage:
for link in page.get_links():
if str([corner * self.scale for corner in list(link["from"])]) == rect:
webbrowser.open(link["uri"])
def process_link_update(self, rect, popup, url_input):
"""Parse a link update event with a new URL"""
for page_link in self.pdfs[self.pdf_id].doc[self.pdfs[self.pdf_id].page_i].links():
if str([corner * self.scale for corner in list(page_link["from"])]) == rect:
page_link["uri"] = url_input.get()
self.pdfs[self.pdf_id].doc[self.pdfs[self.pdf_id].page_i].update_link(page_link)
self.pdfs[self.pdf_id].doc.reload_page(
self.pdfs[self.pdf_id].doc[self.pdfs[self.pdf_id].page_i])
self.set_unsaved()
popup.destroy()
def create_popup(self, popup_title, popup_text, popup_close_message):
"""Create a popup window content dictated by arguments"""
popup = ctk.CTkToplevel(self.root)
popup.title("PyPdfApp")
# Add content to the popup
popup_title_label = ctk.CTkLabel(popup, text=popup_title)
popup_title_label.pack(padx=20, pady=20)
label = ctk.CTkLabel(popup, text=popup_text)
label.pack(padx=20, pady=20)
button = ctk.CTkButton(
popup,
text=popup_close_message,
command=popup.destroy)
button.pack(pady=10)
def window_close_popup(self):
"""Create a popup to offer a chance to save and exit"""
unsaved = self.pdfs.get_unsaved()
if unsaved:
popup = ctk.CTkToplevel(self.root)
popup.title("PyPdfApp - Save & Exit")
# Add content to the popup
title_label = ctk.CTkLabel(popup, text="Save changes before leaving?")
title_label.pack(padx=20, pady=10)
label = ctk.CTkLabel(
popup,
text=f"You have unsaved changes in the following files: \n{','.join(unsaved)}")
label.pack(padx=20, pady=10, fill="x")
back_button = ctk.CTkButton(
popup,
text="Go back",
command=popup.destroy)
back_button.pack(pady=0, side="left")
exit_button = ctk.CTkButton(
popup,
text="Close anyway",
command=sys.exit) # Close button.
exit_button.pack(pady=0, side="right")
popup.resizable(False, False)
popup.grab_set()
else:
sys.exit()
# Save Status
def set_saved(self):
"""Update the file and GUI for a save event"""
if self.pdfs[self.pdf_id].mods_made:
self.pdfs.set_saved(self.pdf_id)
self.file_select_bar.configure(values=self.pdfs.get_keys())
self.update_file_select()
def set_unsaved(self):
"""Update the file and GUI for a new modification"""
if not self.pdfs[self.pdf_id].mods_made:
self.pdfs.set_unsaved(self.pdf_id)
self.file_select_bar.configure(values=self.pdfs.get_keys())
self.update_file_select()
# Boolean Checks
def has_open_pdf(self, *_args):
"""Return True if a PDF is open in the GUI"""
try:
return isinstance(self.pdfs[self.pdf_id], PdfDocInstance)
except:
return False
# STARTUP CHECKS FUNCTIONALITY
# Update
def on_startup_update_check(self):
"""Verify that the software is up to date with the most recent version"""
# Load from settings.json's data.
settings_url = self.settings["newest_version_settings_url"]
version = self.settings["version"]
try:
# Get the data at "newest_version_settings_url" as JSON data.
url_data = requests.get(settings_url, timeout=10).json()
if url_data["version"] != version:
return True
return False
except: # Request failed due to lack of internet connection or missing online file.
return False
def update_skip_event(self):
"""Process an update skip button event"""
try: # Frame exists, destroy it and its contents.
self.update_area.destroy()
except: # Frame was not created, should never happen unless source code is tampered with.
pass
def update_web_event(self):
"""Process an web update button event"""
webbrowser.open(self.settings["newest_version_url"])
sys.exit()
# License
def on_startup_license_check(self):
"""Verify that the user has already agreed to the MIT License terms and conditions"""
if self.settings["license_agreed_to"] is True: # The license has been agreed to.
return True
return False
def license_agree_event(self):
"""Process a license agreement button event"""
try: # Frame exists, destroy it and its contents.
self.license_area.destroy()
self.settings["license_agreed_to"] = True
with open("settings.json", "w", encoding="utf-8") as json_settings:
json.dump(self.settings, json_settings, indent=4)
json_settings.save()
except: # Frame was not created, should never happen unless source code is tampered with.
pass
# MENU FUNCTIONALITY
# Window Exit Event
def app_exit_event(self):
"""Process an appliction exit event"""
if self.has_open_pdf():
self.window_close_popup()
else:
sys.exit()
# Top Menu
def set_menu(self, choice):
"""Proces a menu selection event"""
self.menu = self.menus[choice]
updated_texts = self.menu.get_button_texts()
updated_binds = self.menu.get_button_commands()
updated_states = self.menu.get_button_states()
self.menu_button_1.configure(
text = updated_texts[0],
command=updated_binds[0],
state = updated_states[0])
self.menu_button_2.configure(
text = updated_texts[1],
command=updated_binds[1],
state = updated_states[1])
self.menu_button_3.configure(
text = updated_texts[2],
command=updated_binds[2],
state = updated_states[2])
self.menu_button_4.configure(
text = updated_texts[3],
command=updated_binds[3],
state = updated_states[3])
def previous_page(self, *_args):
"""Change the page (-)"""
page_i = self.pdfs[self.pdf_id].page_i
if page_i-1 >= 0:
self.pdfs[self.pdf_id].page_i = page_i-1
self.update_page(self.pdfs[self.pdf_id].page_i)
def next_page(self, *_args):
"""Change the page (+)"""
page_i = self.pdfs[self.pdf_id].page_i
if page_i+1 <= len(self.pdfs[self.pdf_id].doc)-1:
self.pdfs[self.pdf_id].page_i = page_i+1
self.update_page(self.pdfs[self.pdf_id].page_i)
def save_event(self, *_args):
"""Process a save event"""
self.save_path = save_pdf(self.pdfs[self.pdf_id], forced_save=False)
if self.save_path is not None:
self.set_saved()
def save_pdf(self, _event):
"""Save the modified pdf document"""
self.save_path = save_pdf(self.pdfs[self.pdf_id])
if self.save_path is not None:
self.set_saved()
def open_new_pdf(self, *_args):
"""Open a new PDF with the file selector"""
num_current_keys = len(list(self.pdfs.get_keys()))
result = open_pdf()
if result is not None: # Check that a file was actually selected.
file_path, doc, password = result
new_pdf = PdfDocInstance(file_path, doc, password)
new_id = self.pdfs.add_pdf(new_pdf)
self.pdf_id = new_id
self.file_selected = f"{new_id}"
self.update_file_select()
self.file_select_bar.configure(values=self.pdfs.get_keys())
if num_current_keys < 1:
self.enable_all_buttons()
self.enable_all_keybinds()
num_pages = len(self.pdfs[self.pdf_id].doc)
self.quickset_canvas.config(scrollregion=(0, 0, 250, num_pages * 320))
self.quickset_scrollbar.set(0,(1/num_pages))
self.update_page(self.pdfs[self.pdf_id].page_i)
self.load_quickset()
def load_quickset(self):
"""Use multithreading to load the quickset images on the fly, as large files take a while"""
self.thread = threading.Thread(target=self.update_page_quickset_images)
self.thread.start()
def open_blank_pdf(self, *_args):
"""Open a new PDF with one blank page"""
num_current_keys = len(list(self.pdfs.get_keys()))
file_path = "New File"
doc = create_blank_pdf()
password = ""
new_pdf = PdfDocInstance(file_path, doc, password)
new_id = self.pdfs.add_pdf(new_pdf)
self.pdf_id = new_id
self.file_selected = f"{new_id}"
self.update_file_select()
self.file_select_bar.configure(values=self.pdfs.get_keys())
if num_current_keys < 1:
self.enable_all_buttons()
self.enable_all_keybinds()
self.update_page(self.pdfs[self.pdf_id].page_i)
self.load_quickset()
def close_current_pdf(self, *_args):
"""Close the current PDF only"""
if not self.has_open_pdf():
return
current_keys_list = self.pdfs.get_keys()
num_current_keys = len(current_keys_list)
current_index = current_keys_list.index(self.file_selected)
self.pdfs.remove_pdf(self.pdf_id)
self.file_select_bar.destroy()
self.file_select_bar = ctk.CTkSegmentedButton(
self.file_menu,
command=self.file_selector_callback)
self.file_select_bar.grid(
row=0,
column=0,
padx=(0, 0),
pady=(10, 10),
columnspan=12,
sticky="ew")
self.file_select_bar.configure(values=self.pdfs.get_keys())
self.quickset_canvas.delete("all") # Start with an empty canvas.
if num_current_keys <= 1: # Handle closing of last file.
self.page_count.configure(text="Page: 0/0")
self.disable_all_buttons()
self.disable_all_keybinds()
self.pdf_canvas.delete('all')
else:
if current_index > 0: # Switch to the file to the left.
self.pdf_id = self.pdfs.get_keys()[current_index - 1].replace('*','')
self.update_page(self.pdfs[self.pdf_id].page_i) # Check.
else: # Switch to the file to the right.
self.pdf_id = self.pdfs.get_keys()[current_index].replace('*','')
self.update_page(self.pdfs[self.pdf_id].page_i)
self.update_file_select()
self.load_quickset()
def scale_up(self, *_args):
"""Process a scale up event"""
if self.scale < int(self.settings["app_max_zoom_scale"]):
if self.scale == 0.25: # Enable scale down.
self.zoom_minus.configure(state="normal")
self.scale += 0.25
if self.scale == int(self.settings["app_max_zoom_scale"]): # Disable scale up.
self.zoom_plus.configure(state="disabled")
self.update_scale()
def scale_down(self, *_args):
"""Process a scale down event"""
if self.scale > 0.25:
if self.scale == int(self.settings["app_max_zoom_scale"]): # Enable scale up.
self.zoom_plus.configure(state="normal")
self.scale -= 0.25
if self.scale == 0.25: # Disable scale down.
self.zoom_minus.configure(state="disabled")
self.update_scale()
# File Select Menu
def file_selector_callback(self, value):
"""Change which PDF is being viewed currently"""
self.pdf_id = value.replace('*', '')
self.file_selected = value
self.file_select_bar.set(value)
self.update_page(self.pdfs[self.pdf_id].page_i)
self.load_quickset()
# DISPLAY UPDATE FUNCTIONALITY
def update_page_quickset_images(self, *_args):
"""Update the images based on the scroll position of the h_scrollbar."""
# Only call this function using threading,
# due to running in O(n) with the document page number as n.
try:
num_pages = len(self.pdfs[self.pdf_id].doc)
self.quickset_canvas.delete("all") # Start with an empty canvas.
self.quickset_canvas.config(scrollregion=(0, 0, 250, num_pages * 320))
_preview_width = 200
_center_x = self.quickset_canvas.winfo_width() / 2
_preview_x = (self.quickset_canvas.winfo_width() - _preview_width) / 2
_preview_y = self.quickset_scrollbar.winfo_height()
start_page, end_page = self.quickset_scrollbar.get()
start_page = int(num_pages * math.floor(start_page))
end_page = int(num_pages * math.ceil(end_page))
page_range = end_page - start_page
self.tkimgs = []
for i in range(page_range):
page_i = i + start_page
page = self.pdfs[self.pdf_id].doc[page_i]
pix = page.get_pixmap()
if pix.alpha:
mode = "RGBA"
else:
mode = "RGB"
img = PIL.Image.frombytes(mode, [pix.width, pix.height], pix.samples)
# Resize and prepare the pdf page image.
img = img.resize(
(_preview_width, int(pix.height * (_preview_width/pix.width))),
resample=PIL.Image.Resampling.NEAREST
)
tkimg = PIL.ImageTk.PhotoImage(img)
self.tkimgs.append(tkimg)
# Prevent console warning for CTkLabel with non-CTkImage as "image" argument.
# https://stackoverflow.com/questions/14463277/how-to-disable-python-warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.quickset_canvas.create_text(
_center_x,
i * (300 + 20) + 15,
fill="white",
font="Times 24 bold",
text=f"{i + 1}"
)
self.quickset_canvas.create_image(
_preview_x,
i * (300 + 20) + 35,
image=self.tkimgs[i],
anchor="nw",
tag="pdf_img"
) # Add the image to the canvas.
self.quickset_canvas.config(scrollregion=(0, 0, 250, num_pages * 320))
# Raised when the active PDF is switched while the thread is still running.
except IndexError:
return
def update_quickset(self, *_args):
"""Update the images based on the scroll position of the h_scrollbar."""
canvas_start = (self.quickset_canvas.canvasx(0), self.quickset_canvas.canvasy(0))
canvas_end = (self.quickset_canvas.canvasx(self.quickset_canvas.winfo_width()),
self.quickset_canvas.canvasy(self.quickset_canvas.winfo_height())
)
first_visible_page_i = math.floor((canvas_start[1]) / 320)
last_visible_page_i = math.floor((canvas_end[1]-15) / 320)
print(first_visible_page_i, last_visible_page_i)
visible_pages = []
for i in range(last_visible_page_i - first_visible_page_i + 1):
if first_visible_page_i + i + 1 <= len(self.pdfs[self.pdf_id].doc):
visible_pages.append(first_visible_page_i + i)
if self.pdfs[self.pdf_id].page_i not in visible_pages:
visible_pages.append(self.pdfs[self.pdf_id].page_i)
_preview_width = 200
_preview_x = (self.quickset_canvas.winfo_width() - _preview_width) / 2
print(visible_pages)
for i in visible_pages: # Do this for the visible pages and the current page.
page_i = i
page = self.pdfs[self.pdf_id].doc[page_i]
pix = page.get_pixmap()
if pix.alpha:
mode = "RGBA"
else:
mode = "RGB"
img = PIL.Image.frombytes(mode, [pix.width, pix.height], pix.samples)
# Resize and prepare the pdf page image.
img = img.resize(
(_preview_width,
int(pix.height * (_preview_width/pix.width))
),
resample=PIL.Image.Resampling.NEAREST)
scale = _preview_width / pix.width
tkimg = PIL.ImageTk.PhotoImage(img)
# Prevent console warning for CTkLabel with non-CTkImage as "image" argument.
# https://stackoverflow.com/questions/14463277/how-to-disable-python-warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.quickset_canvas.create_image(
_preview_x,
(i + first_visible_page_i) * (320) + 35,
image=tkimg,
anchor="nw",
tag="pdf_img"
) # Add the image to the canvas.
# Update the quickset for various markup types.
self.update_quickset_freehand(page_i, scale, _preview_width, _preview_x)
self.update_quickset_highlight(page_i, scale, _preview_width, _preview_x)
self.update_quickset_redact(page_i, scale, _preview_width, _preview_x)
def update_quickset_freehand(self, page_i, scale, width_x, start_x):
"""Update the freehand drawings for the specified page on the quickset"""
for freehand_point_set in self.pdfs[self.pdf_id].freehand_points[page_i]:
point_set = []
for point in freehand_point_set:
scaled_point = (
point[0] * scale + start_x,
(point[1] * scale) + (page_i)*(320) + 35
)
if (scaled_point[0] < start_x
or scaled_point[0] > width_x+start_x
or scaled_point[1]-(35 + 320 * (page_i)) > 320
or scaled_point[1]-(35 + 320 * (page_i)) < 0):
# Point is outside the page bounding box, and therefore invalid.
pass
else:
point_set.append(scaled_point)
if len(point_set) > 1:
self.quickset_canvas.create_line(point_set, fill="red")
def update_quickset_redact(self, page_i, scale, width_x, start_x):
"""Update the redacts for the specified page on the quickset"""
for redact_rect in self.pdfs[self.pdf_id].redact_points[page_i]:
scaled_rect = (
redact_rect[0] * scale + start_x,
(redact_rect[1] * scale) + (page_i)*(320) + 35,
redact_rect[2] * scale + start_x,
(redact_rect[3] * scale) + (page_i)*(320) + 35)
if (scaled_rect[0] < start_x
or scaled_rect[0] > width_x+start_x
or scaled_rect[1]-(35 + 320 * (page_i)) > 320
or scaled_rect[1]-(35 + 320 * (page_i)) < 0):
# Point is outside the page bounding box, and therefore invalid.
pass
else:
self.quickset_canvas.create_rectangle(
scaled_rect,
fill="black",
outline="black")
def update_quickset_highlight(self, page_i, scale, width_x, start_x):
"""Update the highlights for the specified page on the quickset"""
for highlight_rect in self.pdfs[self.pdf_id].highlight_points[page_i]:
scaled_rect = (
highlight_rect[0] * scale + start_x,
(highlight_rect[1] * scale) + (page_i)*(320) + 35,
highlight_rect[2] * scale + start_x,
(highlight_rect[3] * scale) + (page_i)*(320) + 35)
if (scaled_rect[0] < start_x
or scaled_rect[0] > width_x+start_x
or scaled_rect[1]-(35 + 320 * (page_i)) > 320
or scaled_rect[1]-(35 + 320 * (page_i)) < 0):
# Point is outside the page bounding box, and therefore invalid.
pass
else:
self.quickset_canvas.create_rectangle(
scaled_rect,
fill="yellow",
outline="yellow",
stipple="gray50")
def quickset_canvas_clicked(self, event):
"""Process a click within the quickset canvas, select the correct page"""
event_y = self.quickset_canvas.canvasy(event.y)
if event_y <= 35:
page = 0
else:
page = math.floor(event_y/320)
if page >= len(self.pdfs[self.pdf_id].doc):
return
self.pdfs[self.pdf_id].page_i = page
self.update_page(self.pdfs[self.pdf_id].page_i)
def quickset_on_mousewheel(self, event):
"""Process a scroll event within the quickset canvas, adjust its position"""
self.quickset_canvas.yview_scroll(int(-1*(event.delta/120)), "units")
def update_button_states(self):
"""Wrapper for set_menu, used in update_page"""
self.set_menu(self.mode.get())
def update_file_select(self):
"""Update the file selection menu bar"""
if f"*{self.pdf_id}" in self.pdfs.get_keys():
self.file_select_bar.set(f"*{self.pdf_id}")