Class OfficeRedactor
Permanently overwrites text in Office Open XML documents (DOCX, XLSX, PPTX). Every occurrence of every search term is replaced in place, character for character, with the configured mask character, directly inside the package's XML parts. The replacement is definitive: the matched characters no longer exist anywhere in the output package, so they cannot be recovered by text extraction or by unzipping the file.
public static class OfficeRedactor
- Inheritance
-
OfficeRedactor
- Inherited Members
Examples
Example 1: Redact every occurrence of a name in a DOCX.
using LMKit.Document.OpenXml;
var request = new OfficeRedactionRequest();
request.SearchTerms.Add("John Doe");
OfficeRedactionReport report = OfficeRedactor.RedactToFile("contract.docx", "contract_redacted.docx", request);
Console.WriteLine($"Occurrences replaced: {report.ReplacedOccurrences}");
Example 2: Redact in-memory spreadsheet bytes.
using LMKit.Document.OpenXml;
var request = new OfficeRedactionRequest();
request.SearchTerms.Add("4111 1111 1111 1111");
byte[] source = File.ReadAllBytes("cards.xlsx");
OfficeRedactionResult result = OfficeRedactor.RedactToBytes(source, ".xlsx", request);
File.WriteAllBytes("cards_redacted.xlsx", result.Data);
Remarks
Coverage. Word documents are scanned across the main body, headers, footers, footnotes, endnotes and comments. Spreadsheets are scanned across the shared string table, inline strings and cached cell string values. Presentations are scanned across every slide and its notes. Matches may span formatting runs: the text of a paragraph is matched as one string and the overwrite is written back through the contributing nodes.
Spreadsheet formulas. Formulas are not rewritten; the term replacement applies to cached cell values and shared strings. A formula that recomputes from unredacted inputs would regenerate its output, but the redacted text itself no longer exists in the package.
Document properties. When ScrubDocumentProperties is set (the default), the same term replacement is applied to the core property text values and to the string values of the application properties part.
Empty requests. A request carrying no search terms is a no-op: the original bytes are returned untouched with a zeroed report.
Input modes. File paths, raw bytes plus an extension, and pre-built Attachment objects are accepted, each with synchronous and asynchronous variants. The input is never mutated: the pass edits a memory copy and returns its bytes.
Methods
- RedactToBytes(Attachment, OfficeRedactionRequest, OfficeRedactionOptions, CancellationToken)
Redacts the Office document carried by
attachmentand returns the redacted bytes together with the report. The format is taken from the attachment's detected MIME type, falling back to its name's extension.
- RedactToBytes(byte[], string, OfficeRedactionRequest, OfficeRedactionOptions, CancellationToken)
Redacts in-memory Office document bytes and returns the redacted bytes together with the report.
- RedactToBytes(string, OfficeRedactionRequest, OfficeRedactionOptions, CancellationToken)
Redacts the Office document at
inputPathand returns the redacted bytes together with the report. The format is taken from the path's extension.
- RedactToBytesAsync(Attachment, OfficeRedactionRequest, OfficeRedactionOptions, CancellationToken)
Asynchronously redacts the Office document carried by
attachmentand returns the redacted bytes together with the report.
- RedactToBytesAsync(byte[], string, OfficeRedactionRequest, OfficeRedactionOptions, CancellationToken)
Asynchronously redacts in-memory Office document bytes and returns the redacted bytes together with the report.
- RedactToBytesAsync(string, OfficeRedactionRequest, OfficeRedactionOptions, CancellationToken)
Asynchronously redacts the Office document at
inputPathand returns the redacted bytes together with the report.
- RedactToFile(string, string, OfficeRedactionRequest, OfficeRedactionOptions, CancellationToken)
Redacts the Office document at
inputPathand writes the result tooutputPath. The format is taken from the input path's extension and is preserved in the output.
- RedactToFileAsync(string, string, OfficeRedactionRequest, OfficeRedactionOptions, CancellationToken)
Asynchronously redacts the Office document at
inputPathand writes the result tooutputPath.