When a .NET application runs on Windows, it can use the Windows-specific build of GroupDocs.Redaction for .NET. This tutorial shows how to create a .NET 10 console application targeting net10.0-windows, redact a phrase in a Word document using a regular expression, and save the redacted document.

A complete working version of the project is available on GitHub.

1. Create a .NET Console Project for Windows

You can create the project using Visual Studio or the .NET CLI. The project must target net10.0-windows. NuGet restores the Windows-specific GroupDocs.Redaction.Net100.Windows assembly for this target framework. This build includes Windows-specific dependencies.

Projects targeting net10.0 without the -windows suffix use the cross-platform build.

Using Visual Studio

Create a new Console App and select .NET 10.0 as the target framework. After creating the project, open the project file and set the target framework:

<TargetFramework>net10.0-windows</TargetFramework>

Using the .NET CLI

Create a .NET 10 console application and open the project directory:

dotnet new console -n WindowsSpecificRedaction -f net10.0
cd WindowsSpecificRedaction

Then set the same target framework in WindowsSpecificRedaction.csproj:

<TargetFramework>net10.0-windows</TargetFramework>

The project files at this step are:

WindowsSpecificRedaction/
├── WindowsSpecificRedaction.csproj
└── Program.cs

2. Install GroupDocs.Redaction 26.10

Add GroupDocs.Redaction for .NET 26.10.0 using Visual Studio or the .NET CLI.

Using Visual Studio

  1. Right-click the project in Solution Explorer.
  2. Select Manage NuGet Packages.
  3. Search for GroupDocs.Redaction and install version 26.10.0.

Using the .NET CLI

Run this command from the project directory:

dotnet add package GroupDocs.Redaction --version 26.10.0

The package provides the Redactor class for loading and saving documents, RegexRedaction for redacting regular-expression matches, and the License.IsLicensed property for checking whether a valid license is applied.

3. Add the Word File

Place the Word document in the project directory and name it:

document.docx

In the project file, configure the document to be copied to the build output directory:

<ItemGroup>
  <None Update="document.docx">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup>

The sample document contains the phrase essential tool. The application replaces the matching phrase and creates document_Redacted.docx. The original document.docx remains unchanged.

4. Add the Redaction Code

Open Program.cs and replace its contents with the following code:

using GroupDocs.Redaction;
using GroupDocs.Redaction.Options;
using GroupDocs.Redaction.Redactions;
using System.Text.RegularExpressions;

var license = new License();
// license.SetLicense("your_license_path");

if (!license.IsLicensed)
{
    Console.WriteLine("The library is running in evaluation mode. Some limitations may apply.");
}

using (Redactor redactor = new Redactor("document.docx"))
{
    var regex = new Regex(@"\s+essential tool", RegexOptions.IgnoreCase);
    redactor.Apply(new RegexRedaction(regex, new ReplacementOptions(" [redacted]")));

    var saveOptions = new SaveOptions() { AddSuffix = true, RasterizeToPDF = false };
    var outputFile = redactor.Save(saveOptions);

    Console.WriteLine($"File saved to {outputFile}.");
}

The License.IsLicensed property returns false until SetLicense loads a valid license. Uncomment the SetLicense call and provide the path to your license file to run the application in licensed mode.

The regular expression \s+essential tool matches one or more whitespace characters followed by the phrase essential tool. The search is case-insensitive, so Essential Tool also matches. Each match, including the whitespace before the phrase, is replaced with [redacted].

The RasterizeToPDF property is set to false, so the document retains its Word format. The AddSuffix property is set to true, so the output file is saved with the Redacted suffix:

document_Redacted.docx

The Save method returns the path to the saved file.

5. Run the Application

Using Visual Studio

  1. Open WindowsSpecificRedaction.slnx, or open the project file.
  2. Press F5 to debug or Ctrl+F5 to run without debugging.

When running the application from Visual Studio, the working directory is typically the build output directory. The redacted file is saved to:

bin\Debug\net10.0-windows\document_Redacted.docx

Using the .NET CLI

Run the following command from the project directory:

dotnet run

When using dotnet run, the working directory is the project directory. The application reads document.docx from this directory and saves document_Redacted.docx alongside it.

6. Check the Redacted Document

Open document_Redacted.docx. The phrase essential tool should be replaced with [redacted]. The original document.docx remains unchanged.

You can use the same project to redact other phrases by modifying the regular expression and replacement text.

Licensing

Without a license, GroupDocs.Redaction for .NET 26.10 runs in evaluation mode. A single process can open up to 25 documents. Attempting to open another document throws a TrialLimitationsException. Trial badges are added to the top of each page in saved documents.

A temporary license is available on the GroupDocs temporary license page. A permanent license is available on the GroupDocs purchase page.

Learn more