A GroupDocs.Redaction for .NET sample that shows how to redact sensitive text from a PDF in a Linux Docker container with .NET 10.
The example uses ExactPhraseRedaction to find a phrase in a PDF and cover every match with a black box. It also shows the Linux dependencies required for text rendering and redaction in a minimal Docker image.
This sample demonstrates how to:
- redact an exact phrase from a PDF;
- apply a black box with
ReplacementOptions(Color.Black); - run GroupDocs.Redaction for .NET in a Linux Docker container;
- configure native libraries and fonts required for PDF text rendering;
- save the redacted document as
document_Redacted.pdf.
The sample uses GroupDocs.Redaction 26.9.0 and targets .NET 10 (net10.0).
The base mcr.microsoft.com/dotnet/runtime:10.0 image does not include all native libraries and fonts required by the redaction example on Linux.
The Dockerfile installs the dependencies used by the application:
fontconfigandlibfreetype6for font discovery and text handling;libgdiplusandlibx11-devfor graphics and rendering support;libc6-devand thelibdl.socompatibility link required by the Linux runtime;ttf-mscorefonts-installerandfonts-latoto provide the fonts used by the rendering environment.
It also updates the font cache after installing the fonts. With these dependencies in the container, ExactPhraseRedaction can create the black box redaction when processing the PDF on Linux.
- .NET SDK 10.0
- Docker Desktop
- GroupDocs.Redaction for .NET 26.9.0 — restored from NuGet
- License file — optional. Without a license, the sample runs in evaluation mode.
CrossplatformRedactionDockerLinux/
│
├── Program.cs
├── CrossplatformRedactionDockerLinux.csproj
├── CrossplatformRedactionDockerLinux.slnx
├── Dockerfile
├── document.pdf
└── document_Redacted.pdf
Program.cs— opens the source PDF, redacts text, and saves the result.CrossplatformRedactionDockerLinux.csproj— targetsnet10.0and references GroupDocs.Redaction 26.9.0.Dockerfile— builds the Linux container and installs the required native libraries and fonts.document.pdf— input PDF used by the sample.document_Redacted.pdf— output PDF created after a successful save.
ExactPhraseRedaction searches for the specified phrase. The search is case-insensitive by default. Pass true as the middle argument to enable case-sensitive matching.
ReplacementOptions(Color.Black) replaces the matched text visually with a solid black rectangle instead of replacement text.
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);
}AddSuffix = true saves the result as document_Redacted.pdf.
RasterizeToPDF remains false by default, so the sample saves the document without rasterizing its pages.
Check the result returned by Apply before treating the output as successfully redacted. Save can still create an output file when a redaction fails, so the output should only be used when the redaction status confirms that the rule was applied.
Place document.pdf in the project directory before running the sample.
- Open
CrossplatformRedactionDockerLinux.slnx. - Select Container (Dockerfile) as the startup profile.
- Press F5 to debug or Ctrl+F5 to run without debugging.
The first run builds the Linux Docker image and installs the required native libraries and fonts. Subsequent runs can reuse the existing image.
Visual Studio mounts the project directory into the container. The generated document_Redacted.pdf therefore appears in the project directory next to document.pdf.
The CrossplatformRedactionDockerLinux profile runs the application directly on the host and does not use the Docker image.
From the project directory, build the image:
docker build -t crossplatform-redaction .Run the sample:
docker run --rm -v "${PWD}:/data" -w /data --entrypoint dotnet crossplatform-redaction /app/CrossplatformRedactionDockerLinux.dllThe project directory is mounted at /data and used as the container's working directory. The application reads document.pdf from /data and writes document_Redacted.pdf back to the mounted project directory.
On Windows PowerShell, ${PWD} expands to the current working directory. Run the commands from the project directory.
- Text redaction — exact phrase matching, case-sensitive search, and colored box redaction.
- Redaction basics — redaction results and
Applied,Skipped, andFailedstatuses. - Save in the original format — how
SaveOptionsworks when PDF rasterization is disabled. - System requirements — supported operating systems and .NET versions.
- Evaluation and licensing — evaluation mode and license requirements.
- Migration notes for 26.9 — changes to the drawing API and
GroupDocs.Redaction.Options.Drawing. - GroupDocs.Redaction for .NET 26.9 release notes — changes introduced in version 26.9.