Class PdfRedactor
Permanently removes content from PDF documents. Unlike drawing a black rectangle over content, redaction deletes the underlying data: text glyphs are removed from content streams, image pixels are scrubbed and re-encoded, vector graphics are trimmed, intersecting annotations are deleted, and content nested inside Form XObjects is processed per instance. The removed content cannot be recovered from the output file by text extraction, raw stream inspection, or rendering.
public static class PdfRedactor
- Inheritance
-
PdfRedactor
- Inherited Members
Examples
Example 1: Redact every occurrence of a name.
using LMKit.Document.Pdf;
var request = new PdfRedactionRequest();
request.SearchTerms.Add("John Doe");
var report = PdfRedactor.RedactToFile("contract.pdf", "contract_redacted.pdf", request);
Console.WriteLine($"Matches: {report.SearchMatches}, glyphs removed: {report.RemovedGlyphs}");
Example 2: Redact a fixed region in memory.
using LMKit.Document.Pdf;
using LMKit.Graphics.Geometry;
var request = new PdfRedactionRequest();
request.Areas.Add(new PdfRedactionArea(pageIndex: 0, Rectangle.FromSize(left: 72, top: 90, width: 220, height: 30)));
byte[] source = File.ReadAllBytes("report.pdf");
PdfRedactionResult result = PdfRedactor.RedactToBytes(source, request);
File.WriteAllBytes("report_redacted.pdf", result.Data);
Remarks
Marking. A PdfRedactionRequest describes what to remove: explicit page areas, text search terms (every match is redacted), and optionally the document's pre-existing /Redact annotations, for interoperability with files marked in other tools.
Surviving content is untouched. Text removal works at glyph granularity: glyphs outside the redacted areas keep their exact positions. Shared resources (a Form XObject or image reused by other pages) are cloned before editing so only the targeted instance changes.
Input modes. File paths, raw bytes, streams and pre-built
Attachment objects are accepted, each with
RedactToFile, RedactToStream and RedactToBytes
variants in synchronous and asynchronous forms. All variants return
a PdfRedactionReport describing what was removed.
Methods
- RedactToBytes(Attachment, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF carried by
attachmentand returns the redacted bytes together with the report.
- RedactToBytes(byte[], PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts in-memory PDF bytes and returns the redacted bytes together with the report.
- RedactToBytes(Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF read from
inputStreamand returns the redacted bytes together with the report.
- RedactToBytes(string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF at
inputPathand returns the redacted bytes together with the report.
- RedactToBytesAsync(Attachment, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF carried by
attachmentand returns the redacted bytes together with the report.
- RedactToBytesAsync(byte[], PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts in-memory PDF bytes and returns the redacted bytes together with the report.
- RedactToBytesAsync(Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF read from
inputStreamand returns the redacted bytes together with the report.
- RedactToBytesAsync(string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF at
inputPathand returns the redacted bytes together with the report.
- RedactToFile(Attachment, string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF carried by
attachmentand writes the result tooutputPath.
- RedactToFile(byte[], string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts in-memory PDF bytes and writes the result to
outputPath.
- RedactToFile(Stream, string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF read from
inputStreamand writes the result tooutputPath.
- RedactToFile(string, string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF at
inputPathand writes the result tooutputPath.
- RedactToFileAsync(Attachment, string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF carried by
attachmentand writes the result tooutputPath.
- RedactToFileAsync(byte[], string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts in-memory PDF bytes and writes the result to
outputPath.
- RedactToFileAsync(Stream, string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF read from
inputStreamand writes the result tooutputPath.
- RedactToFileAsync(string, string, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF at
inputPathand writes the result tooutputPath.
- RedactToStream(Attachment, Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF carried by
attachmentand writes the result tooutputStream.
- RedactToStream(byte[], Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts in-memory PDF bytes and writes the result to
outputStream.
- RedactToStream(Stream, Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF read from
inputStreamand writes the result tooutputStream.
- RedactToStream(string, Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Redacts the PDF at
inputPathand writes the result tooutputStream.
- RedactToStreamAsync(Attachment, Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF carried by
attachmentand writes the result tooutputStream.
- RedactToStreamAsync(byte[], Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts in-memory PDF bytes and writes the result to
outputStream.
- RedactToStreamAsync(Stream, Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF read from
inputStreamand writes the result tooutputStream.
- RedactToStreamAsync(string, Stream, PdfRedactionRequest, PdfRedactionOptions, CancellationToken)
Asynchronously redacts the PDF at
inputPathand writes the result tooutputStream.