A Black Box Isn't Redaction: Checks for Real PDF Deletion

draws a black rectangle over the ID number in the PDF, saves it, and sends it to a colleague; the recipient can still use Ctrl+F to search for the complete numb

Black boxes can hide sensitive text on screen while the text stays in the file. If you cover a name, an ID number or another sensitive term before sharing a PDF, the recipient may still be able to search for it. This guide explains why that happens, how to remove the text for real with PyMuPDF, and how to check the result.

Keep the original copies of any important documents, and check your output against the boundaries and acceptance criteria described below.

Can a black box hide sensitive text in a PDF? The short answer

No. A black box is only an overlay, so you must apply the redaction to remove the underlying content, then verify the result.

Your situationWhat to doWhy
You drew a black box over an ID numberCall apply_redactions() after add_redact_annotThe annotation only overlays the page, and the original text stays in the file
The page is rotatedTransform coordinates with page.derotation_matrix, or call page.remove_rotation() firstWrite-operation coordinates must be relative to the unrotated page
The PDF is scanned, with no text layerProcess it with OCR or convert it to static pages, then review each page manuallyThis method does not apply to scanned PDFs
The document is encryptedDecrypt it firstThis method does not apply to encrypted documents
Metadata, bookmarks or attachments contain sensitive dataClear the metadata, clean up the outline and remove embedded filesapply_redactions() works on the page content stream

Why can sensitive words still be searched after a black box is applied?

Suppose you draw a black rectangle over an ID number, save the PDF and send it to a colleague. They can still use Ctrl+F to find the full number. This is not a software bug. It follows from how PDFs are structured.

  • add_redact_annot only overlays: It places an annotation on top of the page content stream. The original text, vector paths and pixel data of embedded images remain in the file.
  • apply_redactions() removes the content: It physically deletes the content objects in the covered region and rewrites the page stream with a fill color.
  • Without the apply step, the box is cosmetic: Visual covering alone is a layering effect, not de-identification. True de-identification must include apply_redactions().

Comparing correct redaction with invalid masking

To show the difference, the example builds a synthetic PDF entirely in memory, with no external files. It has two scenarios: one where red-box annotations are added but not applied, and one where they are both added and applied.

What a correct result looks like

  • After application-level masking, get_text() should no longer return the target term “CONFIDENTIAL-2024”.
  • The rendered Pixmap of that area should be a solid white block, with no leftover strokes or anti-aliased edges.

Error and boundary cases

  • Skipping apply_redactions: If you call add_redact_annot but not apply_redactions, text extraction still returns the full sensitive words. This is the most common logical omission.
  • Rotated pages: If you calculate coordinates on a rotated page, the masking position can shift, which may partly expose sensitive information. PyMuPDF requires write-operation coordinates to be relative to the unrotated page. Either use page.derotation_matrix to transform the coordinates, or call page.remove_rotation() before the operation.

📊 At a glance

AspectAnnotation only (add_redact_annot)Applied (add_redact_annot + apply_redactions())
Original text in the fileRemains and can be searchedRemoved from the covered region
Visual resultBlack box on top of the contentSolid white block in the covered area
Embedded image pixels and vector pathsRemain in the fileContent objects in the region are removed
Suitable for sharing sensitive contentNoYes, once the checks below pass

PyMuPDF redaction and double-check code

The following Python example uses the PyMuPDF (fitz) library. Run it in a locked dependency environment, such as PyMuPDF>=1.23.0. The code builds the PDF entirely in memory, with no network requests or file I/O, so it suits integration tests or unit checks in CI pipelines.

This script demonstrates the logic only. It is not a production-grade data masking tool. Prepare your own real documents and assess their compliance independently.

import fitz  # PyMuPDF

def create_sample_pdf_with_sensitive_text():
    doc = fitz.open()
    page = doc.new_page()
    page.insert_text((72, 72), "Public info here.", fontsize=12)
    page.insert_text((72, 100), "CONFIDENTIAL-2024", fontsize=12)
    return doc

def redact_and_verify(doc, target_text="CONFIDENTIAL-2024"):
    page = doc[0]
    rects = page.search_for(target_text)
    if not rects:
        raise ValueError(f"Target text '{target_text}' not found")

    # 添加脱敏注释(仅标记,未删除)
    for rect in rects:
        page.add_redact_annot(rect, fill=(1, 1, 1))  # white fill

    # 关键步骤:物理应用脱敏(images=2表示像素级擦除,graphics=1表示移除被覆盖的矢量图形)
    page.apply_redactions(images=2, graphics=1)

    # 验证1:文本层检查
    extracted = page.get_text()
    assert target_text not in extracted, "Text layer still contains sensitive data"

    # 验证2:渲染层检查(可选,用于视觉确认)
    pix = page.get_pixmap(dpi=144)
    # 实际项目中可保存pix或比对像素值,此处仅作流程示意
    return True

# 合成示例执行
sample_doc = create_sample_pdf_with_sensitive_text()
redact_and_verify(sample_doc)

The assert statement confirms that the text-layer cleanup took effect. If it fails, either the desensitization was not performed or the coordinates are wrong. The code does not assess rendering quantitatively, so check the output by eye during manual review or through automated visual regression tests. Some font-rendering engines may keep glyph-outline caches after the text is removed, and text extraction alone cannot detect that.

Acceptance criteria and manual review limits

Passing the assertions does not guarantee that a document is securely delivered. Use the table below during real acceptance testing. It separates a successful code demonstration from effective end-to-end data masking.

Build your own test samples, or use already de-identified files. Do not use production documents that contain real sensitive information.

ObservationPossible causeAction
get_text() contains no sensitive words, but the rendered image shows ghostingFont subset not cleaned, or vector path not overwrittenRerun apply_redactions with the parameters images=2 and graphics=1
The redacted region is offsetPage is rotated and coordinates were not transformedCorrect with derotation_matrix, or call remove_rotation first
Sensitive words in a table are not fully obscuredsearch_for returns several fragmented rectanglesMerge the rectangles manually, or use find_tables to locate cell ranges
Metadata, bookmarks or attachments still contain sensitive informationapply_redactions affects the page content stream onlyAlso call doc.set_metadata({}), clean up the outline and remove embedded files

Where this method does not apply

This method does not apply to:

  • Scanned PDFs without a text layer.
  • Encrypted documents, which must be decrypted first.
  • Dynamic content built from complex form fields or XFA.

For these cases, process the content with OCR or convert it to static pages first. After de-identification, review each page manually. The code only verifies that text strings have disappeared. It cannot replace a full assessment of visual integrity, semantic context or links between objects that could leak information.

🎯 Which approach fits you?

If you are redacting a rotated page

Transform your coordinates with page.derotation_matrix, or call page.remove_rotation() before you apply the redaction, so the box lands where you intend.

If your PDF is scanned or encrypted

Do not rely on this method directly. Run OCR or convert the pages to static pages first, or decrypt the file, and then review every page by hand.

If your PDF contains tables or embedded images

Check the output carefully. Fragmented search rectangles may need merging, and you should confirm that no image pixels or font remnants survive.

Reference and next steps

Next, in your test environment:

  1. Reproduce the verification process with a composite PDF that contains rotated pages, tables and embedded images.
  2. Record how each combination of apply_redactions parameters affects output file size and rendering quality.
  3. Write an internal data masking acceptance checklist. Specify which checks can be automated and which need manual approval.

Using this checklist for document translation

Before you translate, confirm that the page order, source locations and text integrity are correct. Then submit the same sample for translation. Afterwards, check that headings, body text and figure captions still correspond correctly. Do not judge the result only by whether the text reads smoothly.

OmniDoc translates PDF, Word, PowerPoint and Excel files: scanned pages are recognized first, table cells are translated one by one so tables keep their rows and columns, and a PDF comes back as both a bilingual and a translation-only file. Paid translation adds a glossary and context translation.

❓ FAQ

1. Why can someone still search for text I covered with a black box?

The box is an annotation layered over the page. The original text remains in the file until you call apply_redactions(), which removes the underlying content.

2. What happens if I call add_redact_annot but skip apply_redactions?

Text extraction still returns the full sensitive words, so the data is still exposed. This is the most common mistake.

3. Why is my redacted box in the wrong place on a rotated page?

PyMuPDF expects coordinates relative to the unrotated page. Transform them with page.derotation_matrix, or call page.remove_rotation() before the operation.

4. Is this method enough for scanned or encrypted PDFs?

No. Scanned PDFs without a text layer and encrypted documents are outside its scope. Process scanned files with OCR or convert them to static pages, decrypt encrypted files first, and review the results manually.

📝 Summary

A black box only hides text on screen, so the content stays in the file until you apply the redaction. Use apply_redactions(), verify the result with text extraction and rendering checks, and review metadata, bookmarks and attachments as well.

Try it now: upload a document and get it translated with the layout intact

Updated 2026-11-29