Redact text on Linux with Docker

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Redaction MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-redaction -- dnx GroupDocs.Redaction.Mcp --yes
Folders, licensing and more →

You can use GroupDocs.Redaction for Node.js via Java to redact text from a PDF in a Linux Docker container. This tutorial shows how to create a Node.js application, find an exact phrase, cover it with a black box, and run the application in Docker. The same approach can be used with other supported document formats.

Create a Node.js project with Docker support

Create a Node.js project and add a Dockerfile to the project root.

mkdir PdfRedactionDocker
cd PdfRedactionDocker
npm init -y

The project layout is:

PdfRedactionDocker/
├── Dockerfile
├── package.json
├── document.pdf
└── index.js

Install GroupDocs.Redaction

Add @groupdocs/groupdocs.redaction to the project:

npm install @groupdocs/groupdocs.redaction@26.11.0

See Installation for JAVA_HOME setup. The package provides the Redactor class for loading and processing documents, along with the classes and options needed to find and redact sensitive content.

Add the PDF file

Place the PDF you want to process in the project directory and name it document.pdf. The application searches this file for the target phrase and creates document_Redacted.pdf with the redacted content. In this example, the PDF contains the word gap.

Add the redaction code

Replace the contents of index.js with the following code:

const redaction = require('@groupdocs/groupdocs.redaction');
const java = require('java');
const Color = java.import('java.awt.Color');

const redactor = new redaction.Redactor('document.pdf');
try {
  redactor.apply(new redaction.ExactPhraseRedaction('gap', new redaction.ReplacementOptions(Color.BLACK)));

  const saveOptions = new redaction.SaveOptions();
  saveOptions.setAddSuffix(true);
  redactor.save(saveOptions);
} finally {
  redactor.close();
}

document.pdf is the sample file used in this example. Click here to download it.

ExactPhraseRedaction searches the PDF for the specified phrase. The search is case-insensitive by default, so gap and Gap match the same text.

ReplacementOptions with Color.BLACK covers each match with a black box instead of replacing the text with another string.

SaveOptions.setAddSuffix(true) adds _Redacted to the file name, so the result is document_Redacted.pdf. The original document.pdf remains unchanged. See Save in original format for other save options.

apply returns the redaction status. Check this status before treating the output as successfully redacted. save can still create an output file if the redaction was not applied.

Configure the Dockerfile

The following Dockerfile installs Node.js, a JDK, fonts, and native build tools required by the java bridge, then installs npm dependencies.

Dockerfile

FROM eclipse-temurin:17-jdk-jammy

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        curl \
        ca-certificates \
        fontconfig \
        fonts-dejavu-core \
        libfreetype6 \
        python3 \
        make \
        g++ \
    && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && fc-cache -f \
    && rm -rf /var/lib/apt/lists/*

ENV JAVA_HOME=/opt/java/openjdk
ENV PATH="$JAVA_HOME/bin:$PATH"

WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm install --omit=dev

COPY index.js ./

WORKDIR /data
ENTRYPOINT ["node", "/app/index.js"]

Build the application image

From the project directory:

docker build -t PdfRedactionDocker .

Run the container

Mount the project directory into the container and run the application:

docker run --rm \
  -v "${PWD}:/data" \
  -w /data \
  PdfRedactionDocker

The project directory is mounted at /data and used as the working directory. The application reads /data/document.pdf and writes /data/document_Redacted.pdf. Because /data is mapped to the project directory on the host, the redacted PDF appears next to the source file.

On Windows PowerShell, ${PWD} expands to the current directory. Run the command from the project directory.

Check the redacted PDF

Open document_Redacted.pdf. The specified phrase should be covered by a black rectangle, while the original document.pdf remains unchanged.

More resources

Free online document redaction App

You can redact text and other sensitive content in PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, email files, and more with our free online Free Online Document Redaction App.