Skip to content

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

@baofu115

Description

@baofu115

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.

Image

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

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