Redaction
Permanently remove text, images and annotations from PDFs by area, by text, by regular expression or by pattern with NexPDF for Python.
License module
Redaction needs the redaction module (Business tier and above).
Redaction in NexPDF is destructive: the text, images and annotations under the area are removed
from the file, not just covered, and an opaque box is painted over it: black, or white with
fill_white=1. Save the document afterwards.
Redact an area
import nexpdf
with nexpdf.Document.open("statement.pdf") as doc:
doc.apply_redaction(0, 72, 600, 300, 620, 0) # page, llx, lly, urx, ury, fill_white
doc.save("redacted.pdf")Redact every match of a text, an expression or a pattern
redact_matches(query, mode, fill_white) finds and redacts every match in the document and returns
the number of areas redacted.
mode | query is |
|---|---|
0 | Plain text |
1 | A regular expression |
2 | A pattern id: email, phone, credit-card, iban, pt-nif, us-ssn, ipv4, url, date |
n = doc.redact_matches("CONFIDENTIAL", 0, 0)
n += doc.redact_matches(r"\b\d{3}-\d{2}-\d{4}\b", 1, 0)
n += doc.redact_matches("email", 2, 0)
print(f"{n} areas redacted")
doc.save("redacted.pdf")Review before redacting
Use search to show the matches to a reviewer first, for example as highlight annotations, and apply the redaction once they are approved.
Redaction cannot be undone in the saved file. Keep the original if you may need it.