How to Redact PDF Content in TypeScript

How to Redact PDF Content in TypeScript

This guide shows how to redact PDF content with Aspose.PDF FOSS for TypeScript. Page.AddRedact() marks a region; Page.ApplyRedactions() destructively rewrites the content stream so the glyphs underneath every marked region are gone. Marks left unapplied stay as ordinary, copy-selectable annotations. It requires Node.js 22 or later.

Step-by-Step Guide

Step 1: Install the Package

asposefoss/pdf is not yet published — build from source until it ships. See the project README for build instructions.

Verify the installation by importing the Document class in a new TypeScript file — this line should resolve without error once the package is installed:

import { Document } from '@asposefoss/pdf';

Step 2: Import Required Classes

Import Document to open the file; AddRedact() and ApplyRedactions() are called directly on the Page instances it returns:

import { Document } from '@asposefoss/pdf';

Step 3: Mark a Region for Redaction

Page.AddRedact(opts) draws a RedactAnnotation over rect. fill sets the color painted over the region once applied, and overlayText (with align, fontSize, textColor) prints a replacement label such as [REDACTED]:

const doc = Document.OpenFile('memo.pdf');
const page = doc.Pages[0];

page.AddRedact({
  rect: [216, 548, 460, 566],
  fill: [0, 0, 0],
  overlayText: '[REDACTED]',
  align: 'center',
  fontSize: 9,
  textColor: [1, 1, 1],
});

Step 4: Apply Redactions

Page.ApplyRedactions() destructively rewrites the content stream — the glyphs inside every RedactAnnotation marked on the page are gone for good, and the applied marks are removed. Marks added after this call stay unapplied: the value beneath them still reads and is still copy-selectable:

page.ApplyRedactions();
doc.WriteTo('memo-redacted.pdf');

Common Issues and Fixes

A redaction mark shows the black box but the underlying text can still be copied. Page.ApplyRedactions() was never called (or was called before the mark was added). Marking with AddRedact() alone only draws an annotation over the region — the content underneath is destroyed only once ApplyRedactions() runs.

ApplyRedactions() removed content the mark’s rect did not cover. rect is the exact region rewritten — text or images that only partially overlap it can still be partially destroyed at the boundary. Size marks generously around the target content.

Search results cannot be passed straight to Page.AddRedact(). A hit from Page.SearchAnnotationText() carries annotation metadata, not page geometry — it is not the same shape as the rect an AddRedact() mark expects. Page.MarkRedactText(find, opts) and Page.RedactText(find, opts) are the redact-by-text-search entry points, and both are also available at the Document level to sweep every page in one call.

A redaction survives encryption unexpectedly. Redaction and encryption are independent operations — apply redactions before saving with encrypt options if the goal is to both remove the content and restrict access to what remains; applying in the other order does not undo an already-encrypted save.

Frequently Asked Questions

What is the difference between marking and applying a redaction?

AddRedact() only creates an annotation describing where a redaction is planned — the covered content is untouched and still selectable/copyable until ApplyRedactions() runs and destructively rewrites the page.

Can I redact by searching for text instead of specifying a rectangle?

Yes — Page.MarkRedactText(find, opts) marks every match of find for later application, and Page.RedactText(find, opts) marks and applies in one call. Both also exist on Document to sweep the whole document.

Does ApplyRedactions() affect annotations other than the redaction marks?

No — it rewrites the content stream underneath its own RedactAnnotation marks; other annotation types on the page are not touched by this call.

Is a redaction reversible?

No — ApplyRedactions() is destructive. Keep the original file if the unredacted content needs to be recoverable later.

See Also