Redact a PDF Without Leaving the Text Behind
A drawn black box does not remove PDF text. Mark the areas, apply redactions, save with garbage collection, then extract the text again to check.
Updated · Markdown version
Drawing a black box over text does not remove that text from a PDF. The characters stay in the file, and a text extractor still returns them. Redaction removes the content inside each marked area, and you confirm the result by extracting the text again from the saved file.
Scope: the steps below remove matched content from the pages. They do not sanitize the whole file. Metadata, attachments, annotations and form values are separate parts of a PDF and need their own review, covered under Pitfalls.
Steps with PyMuPDF
The steps below use the PyMuPDF library because its redaction behavior is documented on an official page, read on 2026-11-26. Other tools follow the same idea, but check their documentation for the exact commands.
- Open the PDF and find the rectangle around each item you need to remove.
page.search_for("some text")returns a list of rectangles. A string that breaks across two lines comes back as two rectangles, so mark every rectangle in the list. - For each rectangle, call
page.add_redact_annot(rect). The docs call this the first of two steps. It only marks the area. Nothing is removed yet. - After you mark every rectangle on a page, call
page.apply_redactions()on that page. By default it removes every character whose bounding box overlaps a marked rectangle, and it then deletes the redaction annotations themselves. - Set the
imagesargument on purpose. The default blanks out the overlapping image pixels, and option 0 ignores images. Leave images untouched only when you have checked that they hold nothing sensitive. - Set the
graphicsargument on purpose. The default removes overlapping vector graphics. Option 0 ignores graphics, so line art that carries sensitive information can survive. - Save to a new file, not over the original, and pass a
garbagevalue above 0 todoc.save(). The docs say removed text is physically gone from the page "assumingDocument.save()with a suitable garbage option". For images, the default setting writes a new blanked image for the page and does not delete the original, so the original can stay in the file as an unused object until garbage collection removes it.garbage=1removes unused objects andgarbage=4is the highest level.
This script was run on 2026-11-26 with PyMuPDF 2.0 (MuPDF 1.28.5) on a one-page test file that contained the string 078-05-1120:
import pymupdf
doc = pymupdf.open("original.pdf")
for page in doc:
for rect in page.search_for("078-05-1120"):
page.add_redact_annot(rect)
page.apply_redactions()
doc.save("redacted.pdf", garbage=4, deflate=True)Check that it worked
Extract the text of the saved file with a different tool from the one that redacted it, and search for every string you meant to remove. With the Xpdf pdftotext tool, a hyphen as the output name sends the text to the terminal:
pdftotext redacted.pdf -In the test run, a copy of the file with only a black rectangle drawn over the number still returned 078-05-1120 from this command, and so did a copy that was marked but never applied. The redacted file returned the rest of the line without the number.
The extraction check and the rendered pages are evidence for the areas you marked. They are not proof that nothing recoverable remains elsewhere in the file.
Then render the redacted pages as images and look at them. A string that still appears in the extracted text means no redaction rectangle overlapped those characters. Mark a larger area, rerun from the original file, and check again.
Pitfalls
- Box only, no apply step. A filled shape drawn over the text leaves the text in place. So does a redaction annotation that was added but never applied. The extraction check above catches both.
- Rectangle covers only part of a word. Removal is done character by character: a character goes only if its bounding box overlaps a redaction rectangle. In the test run, a rectangle over the left half of
078-05-1120left-1120in the extracted text. The docs also warn that, depending on the font and line height, the overlap rule can remove text you did not mean to remove, so check the neighbouring lines too. - Keeping text. Setting
text=1(PDF_REDACT_TEXT_NONE) keeps the text inside the area. The PyMuPDF docs say this does not comply with the data protection intentions of redaction, so do not use it to hide personal data. - Images. On a scanned page the data sits in image pixels, so text extraction cannot confirm that it is gone. Render the page and look. The default
imagessetting creates a new blanked image for this page only; the docs note that the original image is not deleted or replaced, so other pages may still show it. The remove option (images=1) removes only this page's references to the image, and the image leaves the file only when nothing references it any more and the save uses garbage collection. In the test run, a file saved withgarbage=0after a default image redaction held two image objects, and the same file saved withgarbage=4held one. - Document properties.
apply_redactionsworks on the content of a page. In the test run the author, creator, and producer values were unchanged after redaction. Check those fields separately before sending the file. - Content outside the page. In a second run on 2026-11-27 with PyMuPDF 2.0, a synthetic PDF held the same harmless marker string in its page text, in its metadata and in an embedded text attachment. After the redaction script above and a
garbage=4save, page extraction no longer returned the marker, but the metadata and the attachment still contained it. Before sending a file, also review attached and embedded files, annotations and their replies, form field values, links, JavaScript, thumbnails and XML metadata. PyMuPDF documents a separateDocument.scrub()method whose options name these parts. It was not run for this guide, so if you use it, inspect its output the same way.
When to use a skill instead
A single PDF with a short list of known strings is quicker to redact by hand than to build a script around. A reusable skill may be worth trying when the same kinds of data appear across many files.
sensitive-data-redaction replaces emails, phone numbers, card and bank numbers, keys, IP addresses, and named people in a ticket, log, export, or document, and reports what it replaced. It fits when the PDF text has already been pulled into a text file that will go to an AI tool or another person. Its page lists efficacy as LIFT from a single run on 3 tasks (2026-11-21) and marks that result as provisional, so read it as an early signal, not a full measurement.
pdf-metadata-privacy-check reads the document information entries, such as Author, Creator, and Producer, plus four XMP fields, and flags a name-like pattern in the Author field, email addresses, and Windows or Unix home paths. It reports only and never changes the file. No comparison with and without this skill has been run, so its effect is not measured.
FAQ
Does redacting the pages clean the whole PDF?
No. Applying redactions removes content on the pages. Metadata, attachments, annotations and form values are stored separately. In the test file, the metadata and an embedded attachment still held the removed string after the pages were redacted. Review all of these parts before you send the file.
Is putting black over text the same as redacting it?
No. A drawn box leaves the characters in the file, and text extraction can still read them. Redaction removes the characters that overlap the marked area.
Does redaction also remove images?
It depends on the images setting. The default blanks out only the overlapping pixels in a new copy of the image, option 0 ignores images, and the remove option removes this page's references to the image. Save with a garbage value above 0 so that the unused original can be dropped from the file.
Can I keep the text when I redact?
The option exists, but the PyMuPDF docs say keeping text does not comply with the data protection intentions of redaction. Use the default removal when the goal is to hide sensitive data.
Related skills in this catalog
Platform Score is our assessment under a published method. It is not a safety guarantee.
Related guides
- How to Use AI in Marketing Without Unsupported Claims
- Format SQL with an AI agent without changing its meaning
- Check a Resume for ATS Problems With an AI Agent
- How to Use AI in Excel: Formulas Versus Whole-Sheet Analysis
- How to Use AI to Design a Room: Photo to Brief
- Rewrite a Paragraph with an AI Agent and Keep Its Facts
Terms this guide uses
This item is a worked example in the AI glossary for:
Sources
- https://pymupdf.readthedocs.io/en/latest/page.html (accessed 2026-11-27)
- https://pymupdf.readthedocs.io/en/latest/document.html (accessed 2026-11-27)
- https://www.xpdfreader.com/pdftotext-man.html (accessed 2026-11-27)