Skip to content
Discussion options

You must be logged in to vote

Ah, I should have thought of this earlier 😒
You can add one highlight for any number of rectangles like this:

import fitz

pdf1 = fitz.open("3.pdf")

for page1 in pdf1:
    words = page1.get_text("words")
    word_rects = []
    for word in words:
        word_rects.append(fitz.Rect(word[:4]))
    if word_rects != []:
        page1.add_highlight_annot(word_rects)
    print(f"Added {len(words)} annots on page {page1.number}.")
pdf1.save("3-out.pdf", garbage=3, deflate=True)

This makes a 60 KB file from the original 12 KB. Each page only has one annot - with quads as many as there are words on the page.

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@JorjMcKie
Comment options

@kvrameshreddy
Comment options

Comment options

You must be logged in to vote
3 replies
@kvrameshreddy
Comment options

@JorjMcKie
Comment options

@kvrameshreddy
Comment options

Answer selected by kvrameshreddy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1622 on March 04, 2022 12:15.