當 .NET 應用程式在 Windows 上執行時,它可以使用 GroupDocs.Redaction for .NET 的 Windows 專屬組建。本教學說明如何建立目標為 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
建立新的 Console App,並選取 .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 份文件。嘗試開啟第 26 份文件時會拋出 TrialLimitationsException。評估版會在已儲存文件的每頁頂部加入試用徽章。
臨時授權可於 GroupDocs 臨時授權頁面 取得。永久授權可於 GroupDocs 購買頁面 取得。