Home > Software engineering >  Using PyMuPDF "draw_rect" function is working inconsistent
Using PyMuPDF "draw_rect" function is working inconsistent

Time:10-20

I'm blacking out some information from several PDF's but there are some of these that the rectangles made by "draw_rect" functions are't being drawn correctly. I have checked the rectangles and they look right, that and I'm also usind the "add_redact_annot" with the exact same rectangle and works good.

def hide_text_rects(page, rects):
    for rect in rects:
        page.add_redact_annot(rect)
        page.draw_rect(rect, color=(0,0,0), fill=(0,0,0))

The rectangles seem to be mirrored and zoomed (scaled). I really don't know what to do because I don't find any info related in the docs.

Edit: I found that the PDF's with version 1.7 are the ones working correctly. And the other ones are version 1.5.

CodePudding user response:

The probable reason for this behaviour is a sloppy specification of the page's coordinate system. For example the standard point (0,0) = bottom-left in PDF may have been redefined to be top-left. If this type of coordinate change is not wrapped within PDF stacking operators q / Q (as it should), then any insertions (of text, drawing etc.) appended to the page /Contents act under wrong assumptions, and appear dislocated. Heal this by executing page.clean_contents() before do any insertion. You can also check if this is required at all by page.is_wrapped. Please also consult the documentation - there is an own section dealing with this.

  • Related