Skip to content

About

Redact DOCX text and metadata with GroupDocs.Redaction for Node.js via Java. Covers RegexRedaction, ExactPhraseRedaction, and EraseMetadataRedaction.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Redact Word Documents with Node.js API

Product Page Docs Blog Free Support Temporary License

Overview

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.

Technology Stack

  • Platform: Node.js (via Java bridge)
  • Product: GroupDocs.Redaction for Node.js via Java
  • Language: JavaScript
  • Package: @groupdocs/groupdocs.redaction 26.11.0
  • Native bridge: JDK + node-java / node-gyp (MSVC Build Tools on Windows)

Problem Statement

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.

Solution Overview

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.FileOutputStream writes the redacted file from the Redactor save path

Prerequisites

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.redaction 26.11.0 (API is a Java-bridged flat namespace)
  • License - optional .lic path; without it the sample runs in evaluation mode

Getting Started

Installation

Using Package Manager:

npm install @groupdocs/groupdocs.redaction@26.11.0

Manual Installation:

Clone this repository, run npm install in the project root, and keep documents/document.docx next to the sample entry point.

Configuration

  1. Set licensePath in applyLicense() to your .lic file, or leave the placeholder to run in evaluation mode.
  2. Place the source Word file at documents/document.docx.
  3. Run npm start to write redacted DOCX files under Result/.

When should I use this demo?

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.

Repository Structure

redact-word-documents-nodejs-api/
│
├── package.json
├── documents/
│   └── document.docx
└── src/
    └── index.js

File Descriptions

  • package.json - Declares the npm package and @groupdocs/groupdocs.redaction 26.11.0 dependency
  • documents/document.docx - Sample Word source used by all three redaction methods
  • src/index.js - Entry point with redactWithRegex, redactExactPhrase, and eraseAllMetadata

Code Implementation

Implementation: Replaces text matching a regular expression with a replacement string

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();
  }
}

Technical Details

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 cycle
  • RegexRedaction: Performs pattern-based text replacement
  • ReplacementOptions: Supplies the substitution string for matched text

Parameters:

  • pattern: Regular expression matched against document text
  • replacement: String written in place of each match

Output: Editable DOCX at outputPath with matched text replaced


Implementation: Replaces every occurrence of an exact phrase with a token

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();
  }
}

Technical Details

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 phrases
  • ReplacementOptions: Holds the replacement token string
  • Stream save: RasterizationOptions.setEnabled(false) then redactor.save(FileOutputStream, options)

Parameters:

  • phrase: Exact text to locate in the document
  • replacement: Token written over each match

Output: Editable DOCX at outputPath with the phrase replaced


Implementation: Clears document metadata before the file leaves the redaction step

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();
  }
}

Technical Details

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 categories
  • MetadataFilters.All: Selects every supported metadata filter
  • RedactionStatus: Reports success or failure of the apply step

Parameters:

  • sourcePath: Path to the input DOCX
  • outputPath: Path for the metadata-cleaned DOCX

Output: DOCX at outputPath with metadata fields blanked

Best Practices

When implementing Word redaction with the Node.js API, consider these best practices:

  • Close the redactor: Always call redactor.close() in a finally block to free native handles
  • Check status before save: Skip save when getStatus() returns Failed
  • 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

Additional Resources

For more in-depth information about redacting Word documents with GroupDocs.Redaction for Node.js via Java, explore these technical resources:

Keywords

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

Support

For technical support, visit:

About

Redact DOCX text and metadata with GroupDocs.Redaction for Node.js via Java. Covers RegexRedaction, ExactPhraseRedaction, and EraseMetadataRedaction.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages