当 .NET 应用程序在 Windows 上运行时,它可以使用针对 Windows 的 GroupDocs.Redaction for .NET 构建。本教程展示如何创建一个目标为 net10.0-windows 的 .NET 10 控制台应用程序,使用正则表达式对 Word 文档中的短语进行脱敏,并保存脱敏后的文档。
项目的完整可运行版本可在 GitHub 上获取。
1. 为 Windows 创建 .NET 控制台项目
您可以使用 Visual Studio 或 .NET CLI 创建项目。项目必须以 net10.0-windows 为目标框架。NuGet 会为此目标框架恢复 Windows 特定的 GroupDocs.Redaction.Net100.Windows 程序集。此构建包含 Windows 特定的依赖项。
目标为 net10.0(不带 -windows 后缀)的项目使用跨平台构建。
使用 Visual Studio
创建一个新的控制台应用程序并选择 .NET 10.0 作为目标框架。创建项目后,打开项目文件并设置目标框架:
<TargetFramework>net10.0-windows</TargetFramework>
使用 .NET CLI
创建一个 .NET 10 控制台应用程序并打开项目目录:
dotnet new console -n WindowsSpecificRedaction -f net10.0
cd WindowsSpecificRedaction
然后在 WindowsSpecificRedaction.csproj 中设置相同的目标框架:
<TargetFramework>net10.0-windows</TargetFramework>
此步骤的项目文件如下:
WindowsSpecificRedaction/
├── WindowsSpecificRedaction.csproj
└── Program.cs
2. 安装 GroupDocs.Redaction 26.10
使用 Visual Studio 或 .NET CLI 添加 GroupDocs.Redaction for .NET 26.10.0。
使用 Visual Studio
- 在 Solution Explorer 中右键单击项目。
- 选择 Manage NuGet Packages。
- 搜索
GroupDocs.Redaction并安装版本 26.10.0。
使用 .NET CLI
在项目目录下运行以下命令:
dotnet add package GroupDocs.Redaction --version 26.10.0
该包提供 Redactor 类用于加载和保存文档,RegexRedaction 用于对正则表达式匹配进行脱敏,以及 License.IsLicensed 属性用于检查是否已应用有效许可证。
3. 添加 Word 文件
将 Word 文档放置在项目目录中并命名为:
document.docx
在项目文件中,配置该文档复制到构建输出目录:
<ItemGroup>
<None Update="document.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
示例文档包含短语 essential tool。应用程序会替换匹配的短语并创建 document_Redacted.docx。原始的 document.docx 保持不变。
4. 添加脱敏代码
打开 Program.cs 并将其内容替换为以下代码:
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}.");
}
License.IsLicensed 属性在 SetLicense 加载有效许可证之前返回 false。取消注释 SetLicense 调用并提供许可证文件的路径,以在授权模式下运行应用程序。
正则表达式 \s+essential tool 匹配一个或多个空白字符后跟短语 essential tool。搜索不区分大小写,因此 Essential Tool 也会匹配。每个匹配项(包括短语前的空白)都会被替换为 [redacted]。
RasterizeToPDF 属性设置为 false,因此文档保持 Word 格式。AddSuffix 属性设置为 true,因此输出文件会带有 Redacted 后缀:
document_Redacted.docx
Save 方法返回已保存文件的路径。
5. 运行应用程序
使用 Visual Studio
- 打开
WindowsSpecificRedaction.slnx,或打开项目文件。 - 按 F5 调试或 Ctrl+F5 在不调试的情况下运行。
从 Visual Studio 运行应用程序时,工作目录通常是构建输出目录。脱敏文件保存到:
bin\Debug\net10.0-windows\document_Redacted.docx
使用 .NET CLI
在项目目录下运行以下命令:
dotnet run
使用 dotnet run 时,工作目录是项目目录。应用程序从该目录读取 document.docx 并在同一目录下保存 document_Redacted.docx。
6. 检查脱敏文档
打开 document_Redacted.docx。短语 essential tool 应该被替换为 [redacted]。原始的 document.docx 保持不变。
您可以通过修改正则表达式和替换文本,在同一项目中脱敏其他短语。
许可
如果没有许可证,GroupDocs.Redaction for .NET 26.10 将以评估模式运行。单个进程最多可打开 25 个文档。尝试打开更多文档会抛出 TrialLimitationsException。试用徽章会添加到已保存文档的每页顶部。
临时许可证可在 GroupDocs 临时许可证页面 获取。永久许可证可在 GroupDocs 购买页面 获取。