How to remove the white rectangle left after using `add_redact_annot` + `apply_redactions` to delete text?

Problem Description

When using the PyMuPDF library to modify a PDF file, I’m trying to use add_redact_annot and apply_redactions to remove text in specific areas, then insert new text at those positions. I’ve noticed that after removing the text, a blank white rectangle remains. I’d like to remove this rectangle.

However, I’m encountering the following issue:

When calling the methods in the following order:

# Add a white rectangle covering the entire area annot = page.add_redact_annot(search_rect, fill=(1, 1, 1)) page.apply_redactions() page.delete_annot(annot) 

An error occurs when executing page.delete_annot(annot), with an error message related to the annotation “not being bound to the page”.

If I change the call order and delete the annotation before applying the redaction:

annot = page.add_redact_annot(search_rect, fill=(1, 1, 1)) page.delete_annot(annot) page.apply_redactions() 

Although no error occurs, the apply_redactions() function no longer has the effect of removing the text.

Expected Solution

I would like to understand the correct way to use add_redact_annot and apply_redactions in PyMuPDF to ensure that:

  1. Text in the specified areas can be successfully removed
  2. No white rectangle is left behind in the document

What is the correct method? Is there some additional step required after calling apply_redactions to handle or remove these white rectangles?

pymupdf 1.26.4 Windows11 10.0.26100 Python 3.11.9 

Hi @xuejiangnan - welcome!

You have requested that a white rectangle should be drawn when applying the redaction:
fill=(1, 1, 1)!
Simply do not do that and you should be fine. Removing it once it is drawn is not possible.

This has solved my problem. Thank you very much.