redact-word-documents-nodejs-api is a runnable Node.js demo that redacts DOCX body text and clears document metadata with GroupDocs.Redaction for Node.js via Java. Built on the @groupdocs/groupdocs.redaction 26.11.0 package (Java-bridged flat namespace), the sample applies RegexRedaction, ExactPhraseRedaction, and EraseMetadataRedaction, then saves editable Word output through FileOutputStream with rasterization disabled. It targets Node.js developers who need API-level redaction before documents leave an internal workflow.
I built this when a compliance review asked me to strip both patterned PII and author fields from the same Word packet before it left the team share.
- Platform: Node.js (via Java bridge)
- Product: GroupDocs.Redaction for Node.js via Java
- Language: JavaScript
- Package:
@groupdocs/groupdocs.redaction26.11.0 - Native bridge: JDK + node-java / node-gyp (MSVC Build Tools on Windows)
Developers who ship Word files from Node.js services often must remove sensitive phrases, regex-matched identifiers, and embedded metadata before the document is shared. Hand-rolled string replace on unzipped OOXML breaks runs that span multiple text nodes, misses headers and footnotes, and leaves author, company, and custom properties intact. Format-specific parsers add maintenance cost for every DOCX revision.
GroupDocs.Redaction for Node.js via Java exposes the same Java redaction types through a flat npm namespace, so one Redactor instance can apply text and metadata rules and write an editable DOCX without converting the file to images.
GroupDocs.Redaction addresses these challenges through typed redaction classes and stream-based save options. Key technical features include:
- RegexRedaction: Match and replace body text with a regular expression plus
ReplacementOptions - ExactPhraseRedaction: Replace every occurrence of a fixed phrase with a token string
- EraseMetadataRedaction: Blank author, company, and other fields via
MetadataFilters.All - Editable DOCX save:
RasterizationOptions.setEnabled(false)keeps the Word format intact - Stream output:
java.io.FileOutputStreamwrites the redacted file from theRedactorsave path
Before running these examples, ensure you have:
- Node.js - LTS release with npm available on PATH
- JDK - Java Development Kit installed; required by the node-java bridge
- Build tools - node-gyp and MSVC Build Tools (Windows) or equivalent native toolchain
- Package -
@groupdocs/groupdocs.redaction26.11.0 (API is a Java-bridged flat namespace) - License - optional
.licpath; without it the sample runs in evaluation mode
Using Package Manager:
npm install @groupdocs/groupdocs.redaction@26.11.0Manual Installation:
Clone this repository, run npm install in the project root, and keep documents/document.docx next to the sample entry point.
- Set
licensePathinapplyLicense()to your.licfile, or leave the placeholder to run in evaluation mode. - Place the source Word file at
documents/document.docx. - Run
npm startto write redacted DOCX files underResult/.
Use this demo when a Node.js service must redact DOCX content before export and you need both pattern-based text replacement and metadata wipe in one API. Prefer RegexRedaction for structured identifiers, ExactPhraseRedaction for fixed legal phrases, and EraseMetadataRedaction when author or company fields must not leave the system. Skip it only if you already own a full OOXML rewrite pipeline that covers headers, footers, and properties with equal coverage.
redact-word-documents-nodejs-api/
│
├── package.json
├── documents/
│ └── document.docx
└── src/
└── index.js
- package.json - Declares the npm package and
@groupdocs/groupdocs.redaction26.11.0 dependency - documents/document.docx - Sample Word source used by all three redaction methods
- src/index.js - Entry point with
redactWithRegex,redactExactPhrase, anderaseAllMetadata
Loads the document with Redactor, applies RegexRedaction plus ReplacementOptions, and saves an editable DOCX to outputPath with rasterization disabled via FileOutputStream. Use this when identifiers follow a predictable pattern such as spaced phrases or numbered codes that exact matching cannot cover.
function redactWithRegex(sourcePath, outputPath, pattern, replacement) {
const redactor = new redaction.Redactor(sourcePath);
try {
const result = redactor.apply(
new redaction.RegexRedaction(
pattern,
new redaction.ReplacementOptions(replacement)
)
);
if (result.getStatus() !== redaction.RedactionStatus.Failed) {
const rasterizationOptions = new redaction.RasterizationOptions();
rasterizationOptions.setEnabled(false);
const output = new FileOutputStream(path.resolve(outputPath));
try {
redactor.save(output, rasterizationOptions);
} finally {
output.close();
}
}
return result.getStatus();
} finally {
redactor.close();
}
}The method constructs a RegexRedaction with the caller-supplied pattern and replacement string, applies it through Redactor.apply, then saves only when the status is not Failed. Closing the redactor in finally releases native resources after the stream write completes.
Key Components:
Redactor: Loads the DOCX and hosts the apply/save cycleRegexRedaction: Performs pattern-based text replacementReplacementOptions: Supplies the substitution string for matched text
Parameters:
pattern: Regular expression matched against document textreplacement: String written in place of each match
Output: Editable DOCX at outputPath with matched text replaced
Applies ExactPhraseRedaction with a string ReplacementOptions value and writes the redacted document to outputPath via stream save. Choose this path when legal or policy text is a fixed phrase and regex complexity would add risk without benefit.
function redactExactPhrase(sourcePath, outputPath, phrase, replacement) {
const redactor = new redaction.Redactor(sourcePath);
try {
const result = redactor.apply(
new redaction.ExactPhraseRedaction(
phrase,
new redaction.ReplacementOptions(replacement)
)
);
if (result.getStatus() !== redaction.RedactionStatus.Failed) {
const rasterizationOptions = new redaction.RasterizationOptions();
rasterizationOptions.setEnabled(false);
const output = new FileOutputStream(path.resolve(outputPath));
try {
redactor.save(output, rasterizationOptions);
} finally {
output.close();
}
}
return result.getStatus();
} finally {
redactor.close();
}
}ExactPhraseRedaction searches for the full phrase and substitutes the token from ReplacementOptions. The same stream save keeps the Word format editable so reviewers can open the result in desktop Word without rasterized pages.
Key Components:
ExactPhraseRedaction: Finds and replaces literal phrasesReplacementOptions: Holds the replacement token string- Stream save:
RasterizationOptions.setEnabled(false)thenredactor.save(FileOutputStream, options)
Parameters:
phrase: Exact text to locate in the documentreplacement: Token written over each match
Output: Editable DOCX at outputPath with the phrase replaced
Applies EraseMetadataRedaction with MetadataFilters.All so author, company, and other metadata fields are blanked in the output DOCX. Run this when body text is already clean but document properties would still expose internal ownership.
function eraseAllMetadata(sourcePath, outputPath) {
const redactor = new redaction.Redactor(sourcePath);
try {
const result = redactor.apply(
new redaction.EraseMetadataRedaction(
redaction.MetadataFilters.All
)
);
if (result.getStatus() !== redaction.RedactionStatus.Failed) {
const rasterizationOptions = new redaction.RasterizationOptions();
rasterizationOptions.setEnabled(false);
const output = new FileOutputStream(path.resolve(outputPath));
try {
redactor.save(output, rasterizationOptions);
} finally {
output.close();
}
}
return result.getStatus();
} finally {
redactor.close();
}
}MetadataFilters.All selects the full metadata filter set for the erase operation. The method returns the redaction status so callers can gate further pipeline steps when erase fails.
Key Components:
EraseMetadataRedaction: Clears selected metadata categoriesMetadataFilters.All: Selects every supported metadata filterRedactionStatus: Reports success or failure of the apply step
Parameters:
sourcePath: Path to the input DOCXoutputPath: Path for the metadata-cleaned DOCX
Output: DOCX at outputPath with metadata fields blanked
When implementing Word redaction with the Node.js API, consider these best practices:
- Close the redactor: Always call
redactor.close()in afinallyblock to free native handles - Check status before save: Skip
savewhengetStatus()returnsFailed - Disable rasterization for DOCX: Keep
RasterizationOptions.setEnabled(false)so Word stays editable - Prefer exact phrase for fixed text: Avoid regex when the sensitive string never varies
- Erase metadata last: Clear properties after text redaction so saved packages do not reintroduce author fields
- Provision JDK and node-gyp: Native build failures usually mean the JDK or MSVC tools are missing
For more in-depth information about redacting Word documents with GroupDocs.Redaction for Node.js via Java, explore these technical resources:
-
Step-by-step use case guide in the documentation - Comparison of regex, exact-phrase, and metadata erase methods for DOCX: Read the article ->
-
In-depth blog article about this project - Multi-method tutorial with code and decision guidance: Read the article ->
-
Text redactions for Node.js via Java - Product docs for text redaction types and options: Read the article ->
-
Redact Word text on Windows - Platform notes for Windows hosts running the Node.js via Java API: Read the article ->
-
Redact Word on Windows-specific setups - Related GroupDocs.Redaction blog walkthrough for Word redaction on Windows: Read the article ->
GroupDocs.Redaction, Node.js, RegexRedaction, ExactPhraseRedaction, EraseMetadataRedaction, DOCX, metadata redaction, ReplacementOptions, Redactor, npm @groupdocs/groupdocs.redaction, FileOutputStream, SaveOptions, Node.js via Java, text redaction, Word redaction, RasterizationOptions, MetadataFilters
For technical support, visit: