Skip to content

Unexpected behavior of annot.set_rect() with carets and strikeouts #4755

@charleskolozsvary

Description

@charleskolozsvary

Description of the bug

annot.set_rect() sets the rectangle incorrectly with certain caret annotations. It appears to usually be off just a few points.

Also, attempting to set the rectangle of a strikeout annotation produces the message

cannot set rect: code=4: StrikeOut annotations have no Rect property

which it as the very least not true in my example: I successfully retrieve its rectangle.

How to reproduce the bug

Input

bug.pdf

Image
import pymupdf
from copy import deepcopy

if __name__ == '__main__':
    doc = pymupdf.open('bug.pdf')
    for page in doc:
        caret = page.add_caret_annot((50,50))
        
        for annot in page.annots():
            before_rect = deepcopy(annot.rect)

            def draw_rectangle(rect, c, w):
                drect = page.add_freetext_annot(rect, "", text_color=c)
                drect.set_border(width=w)
                drect.update()

            draw_rectangle(annot.rect, (0,0,1), .3)
            
            print('before:', annot.rect)
            annot.set_rect(annot.rect)
            print('after:', annot.rect)
            
            print('difference:', annot.rect - before_rect)

            draw_rectangle(annot.rect, (0, 1, 0), .3)

            print()
    doc.save('bug-result.pdf')

Output

bug-result.pdf

Image
before: Rect(177.4219970703125, 119.0980224609375, 187.93299865722656, 127.93499755859375)
cannot set rect: code=4: StrikeOut annotations have no Rect property
after: Rect(177.4219970703125, 119.0980224609375, 187.93299865722656, 127.93499755859375)
difference: Rect(0.0, 0.0, 0.0, 0.0)

before: Rect(185.28199768066406, 123.5159912109375, 190.58399963378906, 132.35302734375)
after: Rect(184.22157287597656, 122.45556640625, 191.64442443847656, 133.4134521484375)
difference: Rect(-1.0604248046875, -1.0604248046875, 1.0604248046875, 1.0604248046875)

before: Rect(49.0, 50.5, 69.0, 64.5)
after: Rect(49.0, 50.5, 69.0, 64.5)
difference: Rect(0.0, 0.0, 0.0, 0.0)

What originally created the annotations

The annotations came from Foxit PDF Reader, which is maybe related to the issue (nothing goes wrong on a pymupdf inserted caret, which can be seen in the entire bug-result.pdf), but that's just a guess.

PyMuPDF version

1.26.5

Operating system

MacOS

Python version

3.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions