File size: 34,210 Bytes
433bcaa
 
 
 
77ce35a
 
077cd99
433bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77ce35a
433bcaa
 
 
77ce35a
433bcaa
7544677
433bcaa
 
77ce35a
433bcaa
 
 
 
 
7544677
433bcaa
 
 
 
 
615c7af
433bcaa
 
 
 
 
615c7af
433bcaa
 
 
 
 
 
 
 
615c7af
433bcaa
 
77ce35a
433bcaa
 
 
77ce35a
433bcaa
 
 
77ce35a
433bcaa
 
 
 
77ce35a
433bcaa
 
 
 
 
 
 
77ce35a
433bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77ce35a
433bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77ce35a
433bcaa
 
77ce35a
433bcaa
 
 
77ce35a
433bcaa
 
77ce35a
433bcaa
 
 
c68a8b8
433bcaa
 
 
c68a8b8
433bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c68a8b8
433bcaa
4c2ea84
433bcaa
 
c68a8b8
 
 
433bcaa
c68a8b8
433bcaa
 
 
 
c68a8b8
433bcaa
 
03d32ae
433bcaa
 
c68a8b8
433bcaa
 
c68a8b8
433bcaa
 
 
c68a8b8
433bcaa
c68a8b8
433bcaa
 
c68a8b8
433bcaa
 
c68a8b8
433bcaa
 
 
 
c68a8b8
433bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510f31f
 
433bcaa
 
f0b5e94
 
c68a8b8
77ce35a
 
 
 
 
 
 
 
 
 
433bcaa
77ce35a
 
 
83c31db
433bcaa
 
77ce35a
7268c9c
 
77ce35a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
433bcaa
77ce35a
 
 
433bcaa
77ce35a
f0b5e94
 
 
 
 
 
 
 
 
 
 
 
 
77ce35a
433bcaa
 
 
 
77ce35a
f98ad5c
77ce35a
f98ad5c
 
f0b5e94
 
f98ad5c
f0b5e94
 
 
77ce35a
f0b5e94
 
433bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5d883c
 
 
433bcaa
a5d883c
 
 
 
 
 
 
433bcaa
a5d883c
 
 
9894fac
a5d883c
 
77ce35a
a5d883c
433bcaa
a5d883c
 
433bcaa
a5d883c
 
77ce35a
a5d883c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
433bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
 
 
77ce35a
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
 
 
 
 
 
433bcaa
a5d883c
 
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
 
 
 
 
 
 
 
 
 
 
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
433bcaa
a5d883c
433bcaa
a5d883c
433bcaa
a5d883c
 
 
433bcaa
a5d883c
 
433bcaa
a5d883c
433bcaa
a5d883c
 
433bcaa
a5d883c
433bcaa
a5d883c
 
433bcaa
a5d883c
 
 
 
 
 
 
433bcaa
a5d883c
 
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
 
433bcaa
a5d883c
433bcaa
a5d883c
433bcaa
a5d883c
433bcaa
a5d883c
 
433bcaa
a5d883c
 
 
 
77ce35a
a5d883c
 
 
 
 
 
 
 
433bcaa
a5d883c
433bcaa
77ce35a
a5d883c
77ce35a
a5d883c
 
77ce35a
a5d883c
 
 
 
77ce35a
a5d883c
 
 
77ce35a
a5d883c
 
 
77ce35a
a5d883c
433bcaa
 
 
a5d883c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
433bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77ce35a
 
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
from streamlit import session_state as ss
from streamlit_pdf_viewer import pdf_viewer
import streamlit_pdf_viewer

import streamlit as st


# # Declare variable.
# if 'pdf_ref' not in ss:
#     ss.pdf_ref = None


# # Access the uploaded ref via a key.
# st.file_uploader("Upload PDF file", type=('pdf'), key='pdf')

# if ss.pdf:
#     ss.pdf_ref = ss.pdf  # backup

# # Now you can access "pdf_ref" anywhere in your app.
# if ss.pdf_ref:
#     binary_data = ss.pdf_ref.getvalue()
#     pdf_viewer(input=binary_data, width=700)

# import base64

# def displayPDF(file):
#     # Opening file from file path
#     with open(file, "rb") as f:
#         base64_pdf = base64.b64encode(f.read()).decode('utf-8')

#     # Embedding PDF in HTML
#     pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'

#     # Displaying File
#     st.markdown(pdf_display, unsafe_allow_html=True)

# displayPDF("../Transformers/Bhagavad-Gita-As-It-Is.pdf")


# import streamlit as st
# import streamlit_pdf_viewer

# def displayPDF(file):
#     with open(file, "rb") as f:
#         pdf_bytes = f.read()

#     streamlit_pdf_viewer.pdf_viewer(pdf_bytes)

# displayPDF("../Transformers/Bhagavad-Gita-As-It-Is.pdf")
# Arial Unicode.ttf

# import streamlit as st
# import fitz  # PyMuPDF library
# from PIL import Image, ImageDraw, ImageFont
# import io
# import numpy as np

# def display_pdf_with_highlight(file_path, keywords):
#     # Open the PDF file
#     with fitz.open(file_path) as doc:
#         # Create a new PDF file to hold the highlighted pages
#         highlighted_pdf = fitz.open()

#         # Iterate over each page in the PDF
#         for page_index in range(len(doc)):
#             page = doc.load_page(page_index)
#             pix = page.get_pixmap(dpi=300)
#             img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)

#             # Create a drawing object and highlight the keywords
#             draw = ImageDraw.Draw(img)
#             font = ImageFont.truetype("Arial Unicode.ttf", 14)  # Replace with your desired font
#             for keyword in keywords:
#                 areas = page.search_for(keyword)
#                 for area in areas:
#                     bbox = area.bbox
#                     draw.rectangle(bbox, outline="yellow", width=3)

#             # Convert the highlighted image to a NumPy array
#             img_np = np.asarray(img)

#             # Create a MuPDF-compatible Pixmap from the NumPy array
#             muimg = fitz.Pixmap(fitz.csRGB, img_np.shape[1], img_np.shape[0])
#             muimg.set_data(img_np.tobytes())

#             # Create a new PDF page and insert the highlighted image
#             new_page = highlighted_pdf.new_page(-1, width=muimg.width, height=muimg.height)
#             new_page.insert_image(fitz.Rect(0, 0, muimg.width, muimg.height), stream=muimg)

#         # Create a BytesIO object to hold the highlighted PDF data
#         pdf_bytes = io.BytesIO()
#         highlighted_pdf.write(pdf_bytes)
#         pdf_bytes.seek(0)

#         # Display the highlighted PDF in Streamlit
#         st.download_button(
#             label="Download Highlighted PDF",
#             data=pdf_bytes.getvalue(),
#             file_name="highlighted_pdf.pdf",
#             mime="application/pdf",
#         )

# # Example usage
# file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
# keywords = ["Arjuna", "Krishna"]
# display_pdf_with_highlight(file_path, keywords)

# import pyperclip

# content = str(pyperclip.paste())

# import streamlit as st
# import fitz

# def annotate_pdf(file_path, text_to_highlight):
#     # Open the PDF file
#     with fitz.open(file_path) as doc:
#         # Create a new PDF file to hold the annotated pages
#         annotated_pdf = fitz.open()

#         # Iterate over each page in the PDF
#         for page_index in range(len(doc)):
#             page = doc.load_page(page_index)

#             # Search for the text to highlight
#             areas = page.search_for(text_to_highlight)

#             # Add rectangle annotations for the highlighted areas
#             for area in areas:
#                 page.add_rect_annot(area)

#             # Create a new PDF page and insert the annotated page
#             new_page = annotated_pdf.new_page(-1, width=page.rect.width, height=page.rect.height)
#             new_page.show_pdf_page(page.rect, doc, page_index)

#         # Create a BytesIO object to hold the annotated PDF data
#         pdf_bytes = annotated_pdf.write()

#         # Display the annotated PDF in Streamlit
#         st.download_button(
#             label="Download Annotated PDF",
#             data=pdf_bytes,
#             file_name="annotated_pdf.pdf",
#             mime="application/pdf",
#         )

# # Example usage
# file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
# text_to_highlight = "Arjuna"
# annotate_pdf(file_path, text_to_highlight)

# def displayPDF(file):
#     # Opening file from file path
#     with open(file, "rb") as f:
#         base64_pdf = base64.b64encode(f.read()).decode('utf-8')

#     # Embedding PDF in HTML
#     pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'

#     # Displaying File
#     st.markdown(pdf_display, unsafe_allow_html=True)

# displayPDF("../Transformers/Bhagavad-Gita-As-It-Is.pdf")

# import streamlit as st
    
# import fitz

# def annotate_pdf(file_path, text_to_highlight):
#     # Open the PDF file
#     with fitz.open(file_path) as doc:
#         # Create a new PDF file to hold the annotated pages
#         annotated_pdf = fitz.open()

#         # Iterate over each page in the PDF
#         for page_index in range(len(doc)):
#             page = doc.load_page(page_index)

#             # Search for the text to highlight
#             areas = page.search_for(text_to_highlight)

#             # Add rectangle annotations for the highlighted areas
#             for area in areas:
#                 page.add_rect_annot(area)

#             # Create a new PDF page and insert the annotated page
#             new_page = annotated_pdf.new_page(-1, width=page.rect.width, height=page.rect.height)
#             new_page.show_pdf_page(page.rect, doc, page_index)

#         # Create a BytesIO object to hold the annotated PDF data
#         pdf_bytes = annotated_pdf.write()

#         streamlit_pdf_viewer.pdf_viewer(pdf_bytes)

#         # Display the annotated PDF in Streamlit
#         st.download_button(
#             label="Download Annotated PDF",
#             data=pdf_bytes,
#             file_name="annotated_pdf.pdf",
#             mime="application/pdf",
#         )

# # Example usage
# file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
# text_to_highlight =  "Krishna"
# annotate_pdf(file_path, text_to_highlight)


# import streamlit as st
# import fitz
# import io

# def annotate_pdf(uploaded_file, text_to_highlight):
#     try:
#         # Open the PDF file from the file-like object
#         doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")

#         # Create a new PDF file to hold the annotated pages
#         annotated_pdf = fitz.open()

#         # Iterate over each page in the PDF
#         for page_index in range(len(doc)):
#             page = doc.load_page(page_index)

#             # Search for the text to highlight
#             areas = page.search_for(text_to_highlight)

#             # Add rectangle annotations for the highlighted areas
#             for area in areas:
#                 page.add_rect_annot(area)

#             # Create a new PDF page and insert the annotated page
#             new_page = annotated_pdf.new_page(-1, width=page.rect.width, height=page.rect.height)
#             new_page.show_pdf_page(page.rect, doc, page_index)

#         # Create a BytesIO object to hold the annotated PDF data
#         pdf_bytes = io.BytesIO(annotated_pdf.write())

#         # Display the annotated PDF in Streamlit
#         st.download_button(
#             label="Download Annotated PDF",
#             data=pdf_bytes.getvalue(),
#             file_name="annotated_pdf.pdf",
#             mime="application/pdf",
#         )
#     except Exception as e:
#         st.error(f"An error occurred: {str(e)}")

# # Streamlit app
# def main():
#     st.title("PDF Annotation App")
#     uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
#     if uploaded_file is not None:
#         text_to_highlight = st.text_input("Enter text to highlight")
#         if text_to_highlight:
#             annotate_pdf(uploaded_file, text_to_highlight)

# if __name__ == "__main__":
#     main()


    # file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
    # text_to_highlight =  "Krishna"
    # annotate_pdf(file_path, text_to_highlight)

# import fitz

# import base64

# def displayPDF(file):

#     # Open the PDF document
#     doc = fitz.open("my_pdf.pdf")

#     # Get the first page of the document
#     page = doc.loadPage(4)

#     # Search for the text string to highlight
#     text_to_highlight = "Supreme Personality of Godhead"

#     # Create a rectangle around the text to highlight
#     highlight_rect = fitz.Rect(page.searchFor(text_to_highlight)[0])

#     # Create a highlight annotation
#     highlight_annot = fitz.Annot(page, highlight_rect, "Highlight", {"color": fitz.utils.getColor("yellow")})

#     # Add the annotation to the page
#     page.addAnnot(highlight_annot)

#     # Save the document
#     doc.save("my_pdf_highlighted.pdf")

#     # Opening file from file path
#     with open(file, "rb") as f:
#         base64_pdf = base64.b64encode(f.read()).decode('utf-8')

#     # Embedding PDF in HTML
#     pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'

#     # Displaying File
#     st.markdown(pdf_display, unsafe_allow_html=True)

# displayPDF("../Transformers/Bhagavad-Gita-As-It-Is.pdf")


import streamlit as st
# import fitz
import tempfile

# Import the PDF_ANNOT_HIGHLIGHT constant
# from fitz.PDF_ANNOT import PDF_ANNOT_HIGHLIGHT

import base64
import io


def display_highlighted_pdf(file_path, text_to_highlight):
    # Open the PDF document
    doc = fitz.open(file_path)

    # Iterate over each page in the PDF
    for page_index in range(len(doc)):
        page = doc.load_page(page_index)

        # Search for the text string to highlight
        areas = page.search_for(text_to_highlight)

        # Create a highlight annotation for each area
        for area in areas:
            highlight_rect = fitz.Rect(area)
            highlight_annot = page.add_highlight_annot(highlight_rect)  #fitz.Annot(page.parent, highlight_rect, annot_type=fitz.PDF_ANNOT_HIGHLIGHT)
            highlight_annot.set_colors({"stroke": fitz.utils.getColor("yellow")})
            highlight_annot.update()
            # page.add_annot(highlight_annot)

    # Create a BytesIO object to hold the highlighted PDF data
        # Create a temporary file to save the PDF
    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
        temp_file_path = temp_file.name
        doc.save(temp_file_path)

    # Read the content of the temporary file into a BytesIO object
    with open(temp_file_path, "rb") as f:
        pdf_bytes = io.BytesIO(f.read())

    # # Remove the temporary file
    # st.unlink(temp_file_path)

    # pdf_bytes = io.BytesIO()
    # doc.write(pdf_bytes)
    # pdf_bytes.seek(0)

    # Encode the PDF data as base64
    base64_pdf = base64.b64encode(pdf_bytes.getvalue()).decode('utf-8')

    # Embed the PDF in an HTML iframe
    pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" height="1600" width="680" type="application/pdf"></iframe>'

    # Display the PDF in Streamlit
    st.markdown(pdf_display, unsafe_allow_html=True)
    

# Example usage
file_path = "Bhagavad-Gita-As-It-Is.pdf"
text_to_highlight = "Supreme Personality of Godhead"
# display_highlighted_pdf(file_path, text_to_highlight)



# import streamlit as st

# def display_pdf(pdf_path):
#     # Read the PDF file
#     with open(pdf_path, "rb") as file:
#         pdf_bytes = file.read()

#     # Encode the PDF data as base64
#     base64_pdf = base64.b64encode(pdf_bytes).decode("utf-8")

#     # Embed the PDF in an HTML iframe
#     pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'

#     # Display the PDF in Streamlit
#     st.markdown(pdf_display, unsafe_allow_html=True)

# # Example usage
# pdf_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
# display_pdf(pdf_path)

# import pymupdf

# # Open the PDF file
# doc = pymupdf.open(file_path)

# # Get the first page of the PDF
# # page = pdf_file.pa

# for page in doc: # iterate the document pages
#     text_coordinates = page.search_for("Bhagavad", quads=True)
#     # Highlight the text
#     page.add_highlight_annot(text_coordinates)

# # Get the text of the page
# text = page.get_text()

# # Find the text to highlight
# text_to_highlight = "Bhagavad"

# # Get the coordinates of the text to highlight
# text_coordinates = page.search_for(text_to_highlight)

# Highlight the text
# page.add_highlight_annot(text_coordinates)

# Save the PDF file
# doc.save("example_highlighted.pdf")

import pymupdf
import tempfile
import nltk

nltk.download('stopwords')
from nltk.corpus import stopwords
from collections import Counter
from streamlit_image_zoom import image_zoom
from PIL import Image

def highlight_pdf(file_path, text_to_highlight, page_numbers):
    # Create a temporary file to save the modified PDF
    # temp_pdf_path = "temp_highlighted_pdf.pdf"
    # Create a temporary file to save the modified PDF
    # with tempfile.NamedTemporaryFile(delete=False) as temp_file:
    #     temp_pdf_path = temp_file.name

    # Open the original PDF
    doc = pymupdf.open(file_path)

    pages_to_display = [doc.load_page(page_number - 1) for page_number in page_numbers]

    # Tokenize the text into words
    words = text_to_highlight.split()

   

    # Remove stopwords
    stop_words = set(stopwords.words("english"))
    words = [word for word in words if word.lower() not in stop_words]
    
    print(words)
    
    # Count the frequency of each word
    word_counts = Counter(words)

    # Get the top N most frequent words
    # top_words = [word for word, _ in word_counts.most_common(5)]

    # Iterate over each page in the PDF
    for page in pages_to_display:
        
        # Highlight the specified words on the canvas
        for word in words:
            highlight_rect = page.search_for(word, quads=True)
            # Highlight the text
            # highlight_rect = pymupdf.Rect(word)
        # highlight_annot = page.add_highlight_annot(highlight_rect)
        # highlight_annot.set_colors({"stroke": pymupdf.utils.getColor("yellow")})
        # highlight_annot.update()
            page.add_highlight_annot(highlight_rect)
    
        # Create a new document with only the specified pages
    new_doc = pymupdf.open()
    for page in pages_to_display:
        new_doc.insert_pdf(doc, from_page=page.number, to_page=page.number)

    # Save the modified PDF
    # Save the document to a temporary file
    with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_file:
        temp_pdf_path = temp_file.name
        new_doc.save(temp_pdf_path)
    
    print(temp_pdf_path)

    new_doc.save("example_highlighted.pdf")

    return temp_pdf_path

# Example usage

def pdf_to_images(pdf_path, page_numbers):
    doc = fitz.open(pdf_path)
    images = []
    for page_number in page_numbers:
        page = doc.load_page(page_number - 1)
        pix = page.get_pixmap()
        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
        buf = io.BytesIO()
        img.save(buf, format="PNG")
        byte_im = buf.getvalue()
        images.append(byte_im)
    return images

# Function to display PDF in Streamlit
def display_highlighted_pdf():
    pdf_path = "Bhagavad-Gita-As-It-Is.pdf"
    sources = [7,8]
    response_text = "I offer my respectful obeisances unto the lotus feet of my spiritual master and unto the feet of all Vaiñëavas. I offer my respectful"
    
    highlighted_pdf_path = highlight_pdf(file_path=file_path, text_to_highlight=response_text, page_numbers=sources)

    print(highlighted_pdf_path)

    # with open(highlighted_pdf_path, "rb") as file:
    #     pdf_bytes = file.read()

    #     # Use pdf_viewer to display the PDF in Streamlit
    # pdf_viewer(pdf_bytes, width=700)
    images = pdf_to_images(highlighted_pdf_path, sources)

    for img in images:
        image_zoom(img)

display_highlighted_pdf()


# import streamlit as st
# import streamlit.components.v1 as components
# path_to_html = "https://vedabase.io/en/library/bg/1/1/" 

# # with open(path_to_html,'r') as f: 
# #     html_data = f.read()

# # # Show in webpage
# # st.header("Show an external HTML")
# # st.components.v1.html(html_data)

# try:
#     with open(path_to_html, 'r') as f:
#         # Your file processing code here
#         html_data = f.read()
#         st.header("Show an external HTML")
#         st.components.v1.html(html_data)
# except FileNotFoundError:
#     print("File not found. Please check the file path.")
# except Exception as e:
#     print(f"An error occurred: {e}")


# import streamlit as st
# import requests
# from bs4 import BeautifulSoup

# def fetch_html(url):
#     # Fetch the webpage content
#     response = requests.get(url)
#     if response.status_code == 200:
#         return response.content
#     else:
#         st.error(f"Failed to fetch webpage. Status code: {response.status_code}")

# def scrape_data(html_content):
#     # Parse HTML content
#     soup = BeautifulSoup(html_content, "html.parser")

#     # Scrape data (replace this with your specific scraping logic)
#     data = soup.find_all(class_="container first-container")

#     return data

# def main(url):
#     st.title("Webpage Scraper")

#     # User input for webpage URL
#     url = st.text_input("Enter the URL of the webpage:", value=url)
    
#     # Convert webpage to HTML and scrape data
#     if st.button("Scrape Data"):
#         if url:
#             html_content = fetch_html(url)
#             str_content = """ """
#             if html_content:
#                 data = scrape_data(html_content)
#                 # st.title("HTML Page Display")
#                 # st.components.v1.html(html_content, height=1600, width=800)
#                 # Display scraped data in a new tab
#                 with st.expander("Scraped Data", expanded=True):
#                     for item in data:
#                         # Convert item to string and display
#                         str_content += str(item)
#                         # st.write(str(item))
#                         # st.title("HTML Page Display")
#                         # st.components.v1.html(data, height=1600, width=800)
#                     # st.title("HTML Page Display")
#                     st.components.v1.html(str_content, height=1600, width=680)
# main()


# import streamlit as st

# html_content = """
# <div class="container first-container"> <div class="row"> <div class="col-12 breadcrumb"> <a href="/en/library/">Library</a> »
# <a href="/en/library/bg/">Bhagavad-gītā As It Is</a> » 

#             <a href="/en/library/bg/1/">Chapter One</a>

# </div> </div> <div class="row" id="content" tabindex="-1"> <div class="col-12"> <div class="r r-title r-verse" id="bb181"> <h1>Bg. 1.1</h1> </div> <div class="wrapper-devanagari"> <h2 class="section-title none">Devanagari</h2> <div class="r r-devanagari" id="bb567886">धृतराष्ट्र उवाच<br/>धर्मक्षेत्रे कुरुक्षेत्रे समवेता युयुत्सव: ।<br/>मामका: पाण्डवाश्चैव किमकुर्वत सञ्जय ॥ १ ॥</div> </div> <div class="wrapper-verse-text"> <h2 class="section-title none">Text</h2> <div class="r r-lang-en r-verse-text" id="bb183"><em><em>dhṛtarāṣṭra uvāca</em><br/>dharma-kṣetre kuru-kṣetre<br/>samavetā yuyutsavaḥ<br/>māmakāḥ pāṇḍavāś caiva<br/>kim akurvata sañjaya</em></div> </div> <div class="wrapper-synonyms"> <h2 class="section-title">Synonyms</h2> <div class="r r-lang-en r-synonyms" id="bb184"><p><a href="/en/search/synonyms/?original=dhṛtarāṣṭraḥ"><em>dhṛtarāṣṭraḥ</em></a> <a href="/en/search/synonyms/?original=uvāca"><em>uvāca</em></a> — King Dhṛtarāṣṭra said; <a href="/en/search/synonyms/?original=dharma"><em>dharma</em></a>-<a href="/en/search/synonyms/?original=kṣetre"><em>kṣetre</em></a> — in the place of pilgrimage; <a href="/en/search/synonyms/?original=kuru"><em>kuru</em></a>-<a href="/en/search/synonyms/?original=kṣetre"><em>kṣetre</em></a> — in the place named Kurukṣetra; <a href="/en/search/synonyms/?original=samavetāḥ"><em>samavetāḥ</em></a> — assembled; <a href="/en/search/synonyms/?original=yuyutsavaḥ"><em>yuyutsavaḥ</em></a> — desiring to fight; <a href="/en/search/synonyms/?original=māmakāḥ"><em>māmakāḥ</em></a> — my party (sons); <a href="/en/search/synonyms/?original=pāṇḍavāḥ"><em>pāṇḍavāḥ</em></a> — the sons of Pāṇḍu; <a href="/en/search/synonyms/?original=ca"><em>ca</em></a> — and; <a href="/en/search/synonyms/?original=eva"><em>eva</em></a> — certainly; <a href="/en/search/synonyms/?original=kim"><em>kim</em></a> — what; <a href="/en/search/synonyms/?original=akurvata"><em>akurvata</em></a> — did they do; <a href="/en/search/synonyms/?original=sañjaya"><em>sañjaya</em></a> — O Sañjaya.</p></div> </div> <div class="wrapper-translation"> <h2 class="section-title">Translation</h2> <div class="r r-lang-en r-translation" id="bb185"><p><strong>Dhṛtarāṣṭra said: O Sañjaya, after my sons and the sons of Pāṇḍu assembled in the place of pilgrimage at Kurukṣetra, desiring to fight, what did they do?</strong></p></div> </div> <div class="wrapper-puport"> <h2 class="section-title">Purport</h2> <div class="r r-lang-en r-paragraph" id="bb186"><p><em><a href="/en/library/bg/">Bhagavad-gītā</a></em> is the widely read theistic science summarized in the <em>Gītā-māhātmya</em> (<em>Glorification of the Gītā</em>). There it says that one should read <em><a href="/en/library/bg/">Bhagavad-gītā</a></em> very scrutinizingly with the help of a person who is a devotee of Śrī Kṛṣṇa and try to understand it without personally motivated interpretations. The example of clear understanding is there in the <em><a href="/en/library/bg/">Bhagavad-gītā</a></em> itself, in the way the teaching is understood by Arjuna, who heard the <em>Gītā</em> directly from the Lord. If someone is fortunate enough to understand the <em><a href="/en/library/bg/">Bhagavad-gītā</a></em> in that line of disciplic succession, without motivated interpretation, then he surpasses all studies of Vedic wisdom, and all scriptures of the world. One will find in the <em><a href="/en/library/bg/">Bhagavad-gītā</a></em> all that is contained in other scriptures, but the reader will also find things which are not to be found elsewhere. That is the specific standard of the <em>Gītā.</em> It is the perfect theistic science because it is directly spoken by the Supreme Personality of Godhead, Lord Śrī Kṛṣṇa.</p></div> <div class="r r-lang-en r-paragraph" id="bb187"><p>The topics discussed by Dhṛtarāṣṭra and Sañjaya, as described in the <em>Mahābhārata,</em> form the basic principle for this great philosophy. It is understood that this philosophy evolved on the Battlefield of Kurukṣetra, which is a sacred place of pilgrimage from the immemorial time of the Vedic age. It was spoken by the Lord when He was present personally on this planet for the guidance of mankind.</p></div> <div class="r r-lang-en r-paragraph" id="bb188"><p>The word <em>dharma-kṣetra</em> (a place where religious rituals are performed) is significant because, on the Battlefield of Kurukṣetra, the Supreme Personality of Godhead was present on the side of Arjuna. Dhṛtarāṣṭra, the father of the Kurus, was highly doubtful about the possibility of his sons’ ultimate victory. In his doubt, he inquired from his secretary Sañjaya, “What did they do?” He was confident that both his sons and the sons of his younger brother Pāṇḍu were assembled in that Field of Kurukṣetra for a determined engagement of the war. Still, his inquiry is significant. He did not want a compromise between the cousins and brothers, and he wanted to be sure of the fate of his sons on the battlefield. Because the battle was arranged to be fought at Kurukṣetra, which is mentioned elsewhere in the <em>Vedas</em> as a place of worship – even for the denizens of heaven – Dhṛtarāṣṭra became very fearful about the influence of the holy place on the outcome of the battle. He knew very well that this would influence Arjuna and the sons of Pāṇḍu favorably, because by nature they were all virtuous. Sañjaya was a student of Vyāsa, and therefore, by the mercy of Vyāsa, Sañjaya was able to envision the Battlefield of Kurukṣetra even while he was in the room of Dhṛtarāṣṭra. And so, Dhṛtarāṣṭra asked him about the situation on the battlefield.</p></div> <div class="r r-lang-en r-paragraph" id="bb189"><p>Both the Pāṇḍavas and the sons of Dhṛtarāṣṭra belong to the same family, but Dhṛtarāṣṭra’s mind is disclosed herein. He deliberately claimed only his sons as Kurus, and he separated the sons of Pāṇḍu from the family heritage. One can thus understand the specific position of Dhṛtarāṣṭra in his relationship with his nephews, the sons of Pāṇḍu. As in the paddy field the unnecessary plants are taken out, so it is expected from the very beginning of these topics that in the religious field of Kurukṣetra, where the father of religion, Śrī Kṛṣṇa, was present, the unwanted plants like Dhṛtarāṣṭra’s son Duryodhana and others would be wiped out and the thoroughly religious persons, headed by Yudhiṣṭhira, would be established by the Lord. This is the significance of the words <em>dharma-kṣetre</em> and <em>kuru-kṣetre,</em> apart from their historical and Vedic importance.</p></div> </div> </div> </div> <div class="row d-print-none"> <div class="col-12"> <ul class="mini-pager mt-2 pb-4"> <li class="pager-prev"><a class="btn" href="/en/library/bg/1/"> <i class="fa fa-chevron-left"></i>
# Previous

#                 </a></li>

# <li class="pager-next"><a class="btn" href="/en/library/bg/1/2/">
# Next

#                     <i class="fa fa-chevron-right"></i>

# </a></li>

# </ul> </div> </div> <nav class="rich-menu" id="para-menu"> <div class="Panel" data-csrf-token="l7dMxBge1IaZDbFchwWzWmh1CBpo6pWDY9LKjwSlqmvpDKld3RfTLY85AWyycbUS" data-language="en" data-propose-category-url="/categorization/suggest-category/" data-reload-url="/en/paragraph-tool/?page_id=14054&amp;view_slug=index_view&amp;view_args=&amp;next=/en/library/bg/1/1/&amp;page_url=/en/library/bg/1/1/" id="panel"> <h1 class="pt-title">Paragraph Tools <a class="pt-close-button" href="#" onclick="event.preventDefault();_menu.close();return false;">Close <i class="fa fa-times"></i></a></h1> Please select paragraph first. </div> <div class="Panel" id="subpanel"> <p>Suggest this as category.</p> <a class="link-yes btn btn-lg btn-success px-3" href="">Yes</a> <a class="link-cancel btn btn-lg btn-success px-3" href="">Cancel</a> </div> </nav> <div class="paragraph-tool-button no-touch none"> <div class="paragraph-tool-button-nav"> <div class="paragraph-tool-button-trigger" data-target="#para-menu" id="para-menu-button"> <i class="icon fa fa-briefcase fa-2x"></i> <span class="fa-stack fa"> <i class="fa fa-circle fa-stack-1x p-counter"></i> <span class="p-counter fa-stack-1x fa-stack-text file-text fa-inverse"></span> </span> </div> </div> </div> </div>
# """

# st.title("HTML Page Display")
# st.components.v1.html(html_content, height=1600, width=800)



# import requests
# from bs4 import BeautifulSoup

# # URL of the webpage
# baseurl = "https://vedabase.io/en/library/bg/"

# # Fetch the webpage content
# response = requests.get(baseurl)
# if response.status_code == 200:
#     html_content = response.content

#     # Parse HTML content
#     soup = BeautifulSoup(html_content, "html.parser")

#     # Find all direct child div elements with class="r-chapter"
#     direct_child_div_elements = soup.select("div.col-12 > div.r-chapter")

#     # List to store the extracted text
#     output = []

#     # Iterate over each direct child div element
#     for div in direct_child_div_elements:
#         # Find the <a> tag within the div
#         a_tag = div.find("a")
#         if a_tag:
#             # Extract the text from the <a> tag and append it to the output list
#             output.append(a_tag.text.strip())

#     # Print the output list
#     # print(output)
#     # print(len(output))

# ### Link to all chapters
# import re

# # Sample text
# chapter = output[4]

# text_to_number = {
#     "One": "1",
#     "Two": "2",
#     "Three": "3",
#     "Four": "4",
#     "Five": "5",
#     "Six": "6",
#     "Seven": "7",
#     "Eight": "8",
#     "Nine": "9",
#     "Ten": "10",
#     # Add more numbers if needed
# }

# # Split the text by spaces
# words = chapter.split()

# # Find the text representing the number
# number_text = words[1].strip(":")  # Assuming the number text is the second word

# # Extract the numeric part
# chapter_number = text_to_number[number_text]

# # Print the chapter number
# # print(chapter_number)

# url = baseurl + chapter_number

# # print(url)

# ### all Texts in each chapter

# response = requests.get(url)
# if response.status_code == 200:
#     html_content = response.content

#     # Parse HTML content
#     text = BeautifulSoup(html_content, "html.parser")

#     # print(text)

#     # Find all direct child div elements with class="r-chapter"
#     direct_child_div_elements = text.select("div.col-12 > dl.r.r-verse")

#     # print(direct_child_div_elements)

#     # List to store the extracted text
#     text_number = []

#     # Iterate over each direct child div element
#     for div in direct_child_div_elements:
#         # Find the <a> tag within the div
#         a_tag = div.find("a")
#         if a_tag:
#             # Extract the text from the <a> tag and append it to the output list
#             text_number.append(a_tag.text.strip())

#     # Print the output list
#     # print(text_number)
#     # print(len(text_number))

# ### link to each Text in each chapter
# text_page = text_number[0]

# # Split the text by spaces
# words = text_page.split()

# # Find the text representing the number
# text_number = words[1].strip(":")  # Assuming the number text is the second word

# # print(f"chapter_number - {chapter_number} : text_number - {text_number}")
# texturl = baseurl + chapter_number + "/" + text_number

# # print(texturl)

# main(url=texturl)

# st.title("Display HTML File in Streamlit")

# # Path to the HTML file
# html_file_path = "../Transformers/Bg. 1.1.html"

# try:
#     # Read the HTML file
#     with open(html_file_path, "r", encoding="utf-8") as file:
#         html_content = file.read()
    
#     # Display the HTML content using the 'st.components.v1.html' component
#     expanded = st.checkbox("Expand HTML page")
#     if expanded:
#         st.components.v1.html(html_content, height=1600, width=680)
#     else:
#         st.components.v1.html(html_content, height=600)
# except FileNotFoundError:
#     st.error(f"HTML file '{html_file_path}' not found!")

# import streamlit as st


# st.title("Streamlit Tabs Example")

# # Add tabs to the sidebar
# tabs = st.sidebar.radio("Navigation", ["Home", "About", "Settings"])

# # Display different content based on the selected tab
# if tabs == "Home":
#     st.header("Home Page")
#     st.write("Welcome to the Home page!")

# elif tabs == "About":
#     st.header("About Page")
#     st.write("This is the About page.")

# elif tabs == "Settings":
#     st.header("Settings Page")
#     st.write("Here you can configure your settings.")

# st.title("Netflix-like Grid Display")



# import streamlit as st
# import os
# import streamlit.components.v1 as components

# # Define movie data
# movies = [
#     {"title": "Movie 1", "poster_path": "../Transformers/Bg. 1.1.html"},
#     {"title": "Movie 2", "poster_url": "https://via.placeholder.com/150"},
#     {"title": "Movie 3", "poster_url": "https://via.placeholder.com/150"},
#     {"title": "Movie 4", "poster_url": "https://via.placeholder.com/150"},
#     {"title": "Movie 5", "poster_url": "https://via.placeholder.com/150"},
#     {"title": "Movie 6", "poster_url": "https://via.placeholder.com/150"},
#     {"title": "Movie 7", "poster_url": "https://via.placeholder.com/150"},
#     {"title": "Movie 8", "poster_url": "https://via.placeholder.com/150"},
# ]

# Display movies in a grid
# num_columns = 4
# col_count = 0
# cols = st.columns(num_columns)
# expanded = st.checkbox("Expand HTML page")
# for movie in movies:
#     with cols[col_count % num_columns]:
#         st.markdown(f"<h2>{movie['title']}</h2>", unsafe_allow_html=True)
#         st.write(f"Placeholder for {movie['title']}")
#         if 'poster_path' in movie:
#             # Convert local file path to URL
#             poster_url = f"file://{os.path.abspath(movie['poster_path'])}"
#             print(poster_url)
#             # Display the HTML page using IFrame
#             if expanded:
#                 components.iframe(poster_url, width=800, height=600)
#             else:
#                 components.iframe(poster_url,width=200)
#         else:
#             # Display placeholder image
#             st.image(movie["poster_url"], width=200)
#     col_count += 1