Redaction
Redaction
This page covers permanently removing content from a PDF page. Aspose.PDF
FOSS for TypeScript offers two redaction workflows: a two-phase mark-then-apply
flow you review before committing, and a one-call immediate flow. Both are
available on Page for a single page and on Document for the whole
document.
Marking and Applying Redactions
Page.AddRedact() marks a rectangular region with a /Redact annotation —
the content underneath is untouched and still copy-selectable. Nothing is
actually removed until Page.ApplyRedactions() runs: it destructively
rewrites the content stream for every /Redact mark on the page, paints
each mark’s overlay (its /IC fill and /OverlayText), removes the marks,
and returns the number of redactions applied. Marks added after
ApplyRedactions() is called stay unapplied.
page.AddRedact({
rect: [220, 550, 460, 566],
fill: [0, 0, 0],
overlayText: '[REDACTED]',
align: 'center',
fontSize: 9,
textColor: [1, 1, 1],
});
const applied = page.ApplyRedactions();
console.log('Redactions applied:', applied);Redacting Immediately
Page.Redact() and Page.RedactText() combine marking and applying into a
single call: they truly remove covered text and images, prune resources the
removal leaves orphaned, and paint an opaque marker box over each region —
with no separate ApplyRedactions() step and nothing left to review first.
Page.Redact() takes an explicit list of Rect regions; Page.RedactText()
finds and redacts every occurrence of a search string or RegExp, the same
way Page.SearchAnnotationText() searches. Both accept a RedactOptions
object controlling the marker color, whether to scrubMetadata, and
whether to keepAnnotations that overlap a redacted region.
Because they remove content immediately with no review step, prefer
AddRedact() + ApplyRedactions() when a mark should be inspectable before
it is committed, and Redact() / RedactText() when the regions or search
terms are already known to be safe to remove.
Redacting a Whole Document
The same two workflows exist on Document, scoped to every page at once:
Document.MarkRedactText() marks every occurrence of a search term across
all pages, Document.ApplyRedactions() applies every mark in the document,
and Document.Redact() / Document.RedactText() redact a single page’s
regions or a document-wide search term immediately, mirroring their Page
equivalents.
Tips and Best Practices
- Use
AddRedact()+ApplyRedactions()when a redaction should be reviewable before it takes effect; useRedact()/RedactText()when the regions are already confirmed safe to remove immediately. - Add every
AddRedact()mark for a page before callingApplyRedactions()— marks added afterward are not included in that call and stay unapplied. - Pass
scrubMetadata: trueinRedactOptions/ApplyRedactionsOptionswhen the redacted content might also be present in document metadata (/Infoor XMP). RedactText()andMarkRedactText()search with the same matching rules asSearchAnnotationText()/Search, so a search string that fails to match there will also fail to redact.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| Redaction mark has no effect | Page.ApplyRedactions() was never called | Call Page.AddRedact() for every mark first, then Page.ApplyRedactions() once |
| Content still recoverable after a mark | The mark used AddRedact() (mark-only) without a following ApplyRedactions() call | Call ApplyRedactions(), or use Redact() / RedactText() for one-call removal |
| Redacted text reappears in extracted document metadata | scrubMetadata was not set | Pass { scrubMetadata: true } in the options object |
ApplyRedactions() returns 0 | No /Redact marks existed on the page when it was called | Confirm AddRedact() (or MarkRedactText()) ran first and targeted the same page |
FAQ
What is the difference between AddRedact and Redact?
AddRedact() only marks a region — the content is still present and
copy-selectable until ApplyRedactions() runs. Redact() removes the
content in the same call, with no separate apply step.
Can I redact by searching for text instead of specifying coordinates?
Yes. RedactText() finds and immediately redacts every occurrence of a
search string or RegExp; MarkRedactText() does the same but only marks
the matches, leaving them for a later ApplyRedactions() call.
Does redaction work across the whole document, or only one page at a time?
Both. Page.Redact() / Page.ApplyRedactions() and their siblings operate
on one page; Document.Redact() / Document.ApplyRedactions() and their
siblings operate across every page.
Are annotations near a redacted region removed too?
Only if you let them be. Pass keepAnnotations: true in the options object
to preserve annotations that overlap a redacted region instead of removing
them along with it.
API Reference Summary
| Class/Method | Description |
|---|---|
Page.AddRedact() | Mark a rectangular region with a /Redact annotation, without removing content yet |
Page.ApplyRedactions() | Destructively remove every marked region on the page and return the count applied |
Page.Redact() | Immediately remove content in the given regions, with no separate apply step |
Page.RedactText() | Find and immediately redact every occurrence of a search string or RegExp |
Page.MarkRedactText() | Find and mark (without applying) every occurrence of a search string or RegExp |
Document.ApplyRedactions() | Apply every /Redact mark across the whole document |
Document.Redact() / Document.RedactText() | Immediately redact regions on one page, or a search term across every page |
Document.MarkRedactText() | Mark every occurrence of a search term across every page |