You can use GroupDocs.Redaction for .NET to redact text from a PDF in a Linux Docker container. This tutorial shows how to create a .NET 10 console 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 .NET 10 console project in Visual Studio or with the .NET CLI.
Using Visual Studio
Create a Console App 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 depends on the Visual Studio version). Select Linux as the container operating system. Visual Studio creates the Docker configuration files, including a Dockerfile.
Using the .NET CLI
bash
dotnet new console -n PdfRedactionDocker
cd PdfRedactionDocker
If you use the .NET CLI, add a Dockerfile to the project root manually. The following steps show how to configure it for a Linux container.
Add GroupDocs.Redaction for .NET to the project with Visual Studio or the .NET CLI.
In Visual Studio, open Manage NuGet Packages, search for GroupDocs.Redaction, and install the package.
bash
dotnet add package GroupDocs.Redaction
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 Program.cs with the following code:
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.AddSuffix 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 the Linux libraries and fonts required by the sample for document processing. It is only the base stage, so it does not build or publish the .NET application. You can use it as a starting point or see the complete Dockerfile in the sample repository.
Build the Docker image using the complete Dockerfile. The base stage shown above only prepares the Linux runtime and does not publish the application, so /app/PdfRedactionDocker.dll will not be in the image unless the Dockerfile also includes the build and publish stages.
In Visual Studio, select the Docker profile and start the project. Visual Studio builds the image and runs the application in the Linux container.
From the project directory:
bash
docker build -t PdfRedactionDocker .
Run the container
Mount the project directory into the container and run the published .NET application:
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
You can run the complete sample and see PDF text redaction in a Linux Docker container in our GitHub examples:
Along with the full-featured .NET library, we also provide free online apps for document redaction.
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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.