When you need to redact sensitive text from a PDF on Linux, you can run GroupDocs.Redaction for .NET inside a Docker container. This tutorial shows how to create the project from scratch, add a PDF file, redact an exact phrase with a black box, and run the application in a Linux container with .NET 10.
A complete working version of the project is also available on GitHub.
1. Create a .NET Console Project with Docker Support
You can create the project using Visual Studio or the .NET CLI.
Using Visual Studio
Create a new Console App project and select .NET 10.0 as the target framework. On the additional options screen, enable Enable container support or Add Docker support (the option name may vary depending on your Visual Studio version). Select Linux as the target operating system for the container. Create the project. Visual Studio will generate the necessary Docker configuration files, including a Dockerfile.
Using the .NET CLI
Create a new .NET 10 console application and navigate to the project directory:
dotnet new console -n PdfRedactionDocker
cd PdfRedactionDocker
If you use the CLI, add a Dockerfile to the project root directory manually. You will configure it in the following steps to build, publish, and run the application inside a Linux container.
The resulting project structure should look like this:
PdfRedactionDocker/ ├── Dockerfile ├── PdfRedactionDocker.csproj └── Program.cs
2. Install GroupDocs.Redaction
Add GroupDocs.Redaction for .NET to your project using either Visual Studio or the .NET CLI.
Using Visual Studio
- Right-click the project in Solution Explorer.
- Select Manage NuGet Packages.
- Search for
GroupDocs.Redactionand install the package.
Using the .NET CLI
Run the following command from the project directory:
dotnet add package GroupDocs.Redaction
The package provides the Redactor class for loading and processing documents, along with various classes and options for configuring and applying redactions.
3. Add the PDF File
Place the PDF you want to process in the project directory and name it:
document.pdf
The application will search this file for the phrase to redact and create document_Redacted.pdf with the result.
For this example, the PDF contains the word gap.
4. Add the Redaction Code
Open Program.cs and replace its contents with:
using GroupDocs.Redaction;
using GroupDocs.Redaction.Options;
using GroupDocs.Redaction.Options.Drawing;
using GroupDocs.Redaction.Redactions;
using (Redactor redactor = new Redactor("document.pdf"))
{
redactor.Apply(
new ExactPhraseRedaction(
"gap",
new ReplacementOptions(Color.Black)));
var saveOptions = new SaveOptions() { AddSuffix = true };
var outputFile = redactor.Save(saveOptions);
}
ExactPhraseRedaction searches the PDF for the specified phrase. The search is case-insensitive by default, so gap and Gap match the same text.
ReplacementOptions(Color.Black) applies a black box redaction to each match instead of replacing the text with another string.
SaveOptions.AddSuffix adds _Redacted to the output filename, so the result is saved as:
document_Redacted.pdf
The original document.pdf remains unchanged.
The Apply method returns the redaction status. Check this result in production code before treating the output as successfully redacted. Save can still create an output file if the redaction was not applied.
5. Configure the Dockerfile
Use the following Dockerfile as a sample for building and running your .NET 10 application in a Linux container.
The example includes the steps required to install the Linux libraries and fonts needed by GroupDocs.Redaction for document processing.
You can find the complete Dockerfile here.
FROM mcr.microsoft.com/dotnet/runtime:10.0 AS base
USER root
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
fontconfig \
libfreetype6 \
libgdiplus \
libc6-dev \
libx11-dev \
software-properties-common \
&& echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections \
&& apt-add-repository multiverse \
&& apt-get update \
&& apt-get install -y ttf-mscorefonts-installer fonts-lato \
&& fc-cache -f \
&& dir="$(dirname "$(find /usr/lib /lib -name 'libdl.so.2' 2>/dev/null | head -n1)")" \
&& ln -sf "$dir/libdl.so.2" "$dir/libdl.so" \
&& rm -rf /var/lib/apt/lists/*
USER $APP_UID
WORKDIR /app
The Docker image includes the native libraries, font support, and other dependencies required to run document processing code in a Linux container.
6. Build the Application Image
You can build the Docker image using either Visual Studio or the Docker CLI.
Using Visual Studio
If Docker support was enabled when creating the project, select the Docker profile from the Debug or Run dropdown and start the project. Visual Studio will build the Docker image and run the application in the configured Linux container.
Using the Docker CLI
Open a terminal in the project directory and run:
docker build -t PdfRedactionDocker .
Docker uses the Dockerfile to create the Linux image and install the required dependencies.
7. Run the Container
If you are using the Docker CLI, run the application with the project directory mounted into the container:
docker run --rm \
-v "${PWD}:/data" \
-w /data \
--entrypoint dotnet \
PdfRedactionDocker \
/app/PdfRedactionDocker.dll
The project directory is mounted at /data and used as the container’s working directory. The application therefore reads the source file from:
/data/document.pdf
and saves the redacted file to:
/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 working directory. Run the command from the project directory.
8. Check the Redacted PDF
After the application finishes, open:
document_Redacted.pdf
The specified phrase should be covered by a black rectangle, while the original document.pdf remains unchanged.
You can now use the same approach to integrate PDF text redaction with GroupDocs.Redaction for .NET into a Linux-based Docker application.
Licensing
Without a license, GroupDocs.Redaction for .NET runs in evaluation mode. The evaluation version has usage limits and adds a watermark to the saved document.
A 30-day temporary license is available on the GroupDocs temporary license page. Pricing information is available on the GroupDocs.Redaction for .NET purchase page.