如何使用 C# 對 PDF 中的文字和區域進行遮蔽

如何使用 IronPDF 和 C# 對 PDF 中的文字和區域進行遮蔽

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 讓 C# 開發人員能透過簡單的遮蔽方法,永久移除 PDF 文件中的敏感文字與區域,藉由以黑色方塊或替代文字覆蓋內容,確保資料隱私並符合規範要求。

文本遮蔽是指從文件中永久移除或遮蔽敏感資訊的過程。 通常的做法是將文字覆蓋上黑色方塊,或使用工具將其完全刪除。 "隱去"功能可確保資訊無法被存取或檢視,為敏感內容提供隱私與安全性保障。 在 PDF 文件中,資料遮蔽對於符合 GDPR、HIPAA 及其他隱私標準等資料保護法規至關重要。 IronPDF 提供強大的遮蔽功能,不僅僅是簡單的文字覆蓋,更能確保敏感資料從 PDF 結構中永久移除。

同樣地,對特定區域進行遮蔽處理,會使文件中的指定區域變得模糊不清。 這需要提供區域的座標、寬度和高度。 區域遮蔽功能在處理表單、簽名、圖片或任何包含敏感資訊的視覺內容時特別有用。 與簡單的文字遮蔽不同,基於區域的遮蔽功能讓開發人員能夠針對文件的特定區域進行處理,而不受內容類型的限制。

快速入門:在 PDF 中遮蔽敏感文字

使用 IronPDF 的遮蔽方法從 PDF 文件中移除敏感資訊。 只需幾行程式碼,即可對所有 PDF 頁面中的文字進行遮蔽,確保機密性與合規性。 本指南將示範如何使用 IronPDF 的 API 載入 PDF 檔案、進行遮蔽處理,並儲存更新後的文件。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 請複製並執行此程式碼片段。

    IronPdf.PdfDocument doc = IronPdf.PdfDocument.FromFile("document.pdf");
    doc.RedactTextOnAllPages("sensitive info");
    doc.SaveAs("redacted_document.pdf");
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronPDF

    arrow pointer


如何在 C# 中對 PDF 文件進行文字遮蔽?

使用 IronPDF 進行文字遮蔽非常簡單。 請使用 RedactTextOnAllPages 方法從整份文件中移除指定短語。 讓我們以一份 PDF 範例文件為例。

:path=/static-assets/pdf/content-code-examples/how-to/redact-text-redact-text.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("novel.pdf");

// Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric");

pdf.SaveAs("redacted.pdf");
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")

' Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric")

pdf.SaveAs("redacted.pdf")
$vbLabelText   $csharpLabel

若需更進階的 PDF 處理功能,您可以探索如何編輯 PDF,或瞭解如何設定 PDF 權限與密碼,藉此在進行資料遮蔽的同時,進一步提升文件安全性。

經編輯處理的 PDF 檔案長什麼樣子?

從所有頁面中刪除 Alaric 字串後的 PDF 結果。

請分別使用 RedactTextOnPageRedactTextOnPages 方法,來遮蔽單一或多頁面的文字。

哪些參數會控制文字遮蔽?

以下是 redact 文字方法的參數及其用途:

  • ReplaceText:這是您希望遮蔽的文字字串。
  • CaseSensitive:一個布林值,用於指示搜尋是否應區分大小寫。若為 true,則會精確匹配大寫與小寫字母。 預設值為 false。
  • OnlyMatchWholeWords:一個布林值,用於指定是否僅匹配完整單詞。 預設值為 true。
  • DrawRectangles:一個布林值,用於決定是否在遮蔽區域周圍繪製黑色矩形。 預設值為 true。
  • ReplacementText:此為將取代被遮蔽內容的文字。 預設的替換文字為"*"。

以下是一個更完整的範例,展示如何使用這些參數:

using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("confidential_report.pdf");

// Redact with custom parameters
pdf.RedactTextOnAllPages("SSN: 123-45-6789", 
    caseSensitive: true, 
    onlyMatchWholeWords: false, 
    drawRectangles: true, 
    replacementText: "[REDACTED]");

// Redact multiple sensitive items
string[] sensitiveTerms = { "salary", "password", "credit card" };
foreach (string term in sensitiveTerms)
{
    pdf.RedactTextOnAllPages(term, caseSensitive: false);
}

pdf.SaveAs("fully_redacted_report.pdf");
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("confidential_report.pdf");

// Redact with custom parameters
pdf.RedactTextOnAllPages("SSN: 123-45-6789", 
    caseSensitive: true, 
    onlyMatchWholeWords: false, 
    drawRectangles: true, 
    replacementText: "[REDACTED]");

// Redact multiple sensitive items
string[] sensitiveTerms = { "salary", "password", "credit card" };
foreach (string term in sensitiveTerms)
{
    pdf.RedactTextOnAllPages(term, caseSensitive: false);
}

pdf.SaveAs("fully_redacted_report.pdf");
Imports IronPdf

Dim pdf As PdfDocument = PdfDocument.FromFile("confidential_report.pdf")

' Redact with custom parameters
pdf.RedactTextOnAllPages("SSN: 123-45-6789", 
                         caseSensitive:=True, 
                         onlyMatchWholeWords:=False, 
                         drawRectangles:=True, 
                         replacementText:="[REDACTED]")

' Redact multiple sensitive items
Dim sensitiveTerms As String() = {"salary", "password", "credit card"}
For Each term As String In sensitiveTerms
    pdf.RedactTextOnAllPages(term, caseSensitive:=False)
Next

pdf.SaveAs("fully_redacted_report.pdf")
$vbLabelText   $csharpLabel

若需處理格式複雜的文件,請參考字型管理指南,以確保在遮蔽處理過程中能正確識別文字。


如何在 PDF 中遮蔽特定區域?

在文件中遮蔽特定區域非常有效。請使用 RectangleF 物件呼叫 RedactRegionsOnAllPages 方法,以遮蔽目標文件的指定區域。 讓我們使用上文範例中的同一份 PDF 範例文件

:path=/static-assets/pdf/content-code-examples/how-to/redact-text-redact-region.cs
using IronPdf;
using IronSoftware.Drawing;

PdfDocument pdf = PdfDocument.FromFile("novel.pdf");

RectangleF rectangle = new RectangleF(5, 700, 50, 50);

// Redact region on coordinates(5,700) with width and height 50 pixels
pdf.RedactRegionsOnAllPages(rectangle);

pdf.SaveAs("redactedRegion.pdf");
Imports IronPdf
Imports IronSoftware.Drawing

Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")

Private rectangle As New RectangleF(5, 700, 50, 50)

' Redact region on coordinates(5,700) with width and height 50 pixels
pdf.RedactRegionsOnAllPages(rectangle)

pdf.SaveAs("redactedRegion.pdf")
$vbLabelText   $csharpLabel

進階區域遮蔽範例

處理複雜文件時,您可能需要遮蔽多個區域或動態計算座標:

using IronPdf;
using IronSoftware.Drawing;
using System.Collections.Generic;

PdfDocument pdf = PdfDocument.FromFile("form_with_signatures.pdf");

// Redact multiple regions
List<RectangleF> regionsToRedact = new List<RectangleF>
{
    new RectangleF(100, 200, 200, 50),  // Signature area
    new RectangleF(100, 300, 200, 100), // Address block
    new RectangleF(350, 150, 150, 150)  // Photo ID area
};

foreach (var region in regionsToRedact)
{
    pdf.RedactRegionsOnAllPages(region);
}

// Redact regions on specific pages only
pdf.RedactRegionOnPage(0, new RectangleF(50, 50, 100, 30)); // Page 1 header
pdf.RedactRegionOnPages(new[] { 2, 3, 4 }, new RectangleF(400, 700, 150, 50)); // Footer on pages 3-5

pdf.SaveAs("form_redacted.pdf");
using IronPdf;
using IronSoftware.Drawing;
using System.Collections.Generic;

PdfDocument pdf = PdfDocument.FromFile("form_with_signatures.pdf");

// Redact multiple regions
List<RectangleF> regionsToRedact = new List<RectangleF>
{
    new RectangleF(100, 200, 200, 50),  // Signature area
    new RectangleF(100, 300, 200, 100), // Address block
    new RectangleF(350, 150, 150, 150)  // Photo ID area
};

foreach (var region in regionsToRedact)
{
    pdf.RedactRegionsOnAllPages(region);
}

// Redact regions on specific pages only
pdf.RedactRegionOnPage(0, new RectangleF(50, 50, 100, 30)); // Page 1 header
pdf.RedactRegionOnPages(new[] { 2, 3, 4 }, new RectangleF(400, 700, 150, 50)); // Footer on pages 3-5

pdf.SaveAs("form_redacted.pdf");
Imports IronPdf
Imports IronSoftware.Drawing
Imports System.Collections.Generic

Dim pdf As PdfDocument = PdfDocument.FromFile("form_with_signatures.pdf")

' Redact multiple regions
Dim regionsToRedact As New List(Of RectangleF) From {
    New RectangleF(100, 200, 200, 50),  ' Signature area
    New RectangleF(100, 300, 200, 100), ' Address block
    New RectangleF(350, 150, 150, 150)  ' Photo ID area
}

For Each region In regionsToRedact
    pdf.RedactRegionsOnAllPages(region)
Next

' Redact regions on specific pages only
pdf.RedactRegionOnPage(0, New RectangleF(50, 50, 100, 30)) ' Page 1 header
pdf.RedactRegionOnPages(New Integer() {2, 3, 4}, New RectangleF(400, 700, 150, 50)) ' Footer on pages 3-5

pdf.SaveAs("form_redacted.pdf")
$vbLabelText   $csharpLabel

區域遮蔽效果為何?

生成的 PDF 檔案是透過在座標 (5,700) 處遮蔽一個寬高各為 50 像素的區域所產生。

何時該使用區域遮罩與文字遮罩?

請分別使用 RedactRegionOnPageRedactRegionOnPages 方法,從單一或多頁內容中遮蔽特定區域。

在以下情況下,進行地區化處理是理想的:

  • 請移除圖片、標誌或圖形元素
  • 敏感內容包括手寫筆記或簽名
  • 您處理的是版面配置固定的表單
  • 您希望刪除整段內容,無論其文字內容為何

在以下情況下,建議對文本進行修飾

  • 您正在搜尋特定的關鍵字或短語
  • 需隱去的內容出現在多個位置
  • 您需要區分大小寫或整詞匹配
  • 不同檔案間的文件結構可能有所差異

若要實現全面的 PDF 安全性,請將遮蔽功能與其他安全功能結合使用。 進一步了解 PDF 簽名功能,並探索 PDF 壓縮技術以優化您的加密文件。

PDF 遮蔽的最佳實務

在應用程式中實作資料遮蔽時:

  1. 務必儲存至新檔案:切勿覆寫原始文件,以確保稽核追蹤的完整性
  2. 驗證遮蔽完整性:檢視輸出內容,確保所有敏感資料均已移除
  3. 考量元資料:請記得編輯並移除可能包含敏感資訊的元資料
  4. 測試座標計算:使用區域遮罩功能時,請針對不同頁面尺寸與方向測試座標計算

若需處理其他文件需求,請探索如何在進行遮蔽處理前從 PDF 中擷取文字與圖片,或瞭解如何建立具備內建隱私控制功能的 PDF 表單

準備好探索更多可能性了嗎? 請點此查看我們的教學頁面:編輯 PDF

常見問題

何謂 PDF 文字遮蔽,為何它如此重要?

PDF 文字遮蔽是指透過以黑色方塊覆蓋文字或完全刪除文字,從文件中永久移除或遮蔽敏感資訊的過程。IronPDF 提供強大的遮蔽功能,可確保敏感資料從 PDF 結構中永久移除,有助於符合 GDPR 和 HIPAA 等資料保護法規。

如何從 PDF 文件的所有頁面中刪除特定文字?

using IronPDF,您可以透過 RedactTextOnAllPages 方法從所有頁面中遮蔽特定文字。只需載入您的 PDF 文件,呼叫 doc.RedactTextOnAllPages('敏感資訊'),然後儲存已遮蔽的文件。此操作將永久移除整個 PDF 文件中指定的文字。

我可以對 PDF 文件中的特定區域或範圍進行遮蔽處理嗎?

是的,IronPDF 允許您透過 RedactRegionsOnAllPages 方法,指定座標、寬度和高度來遮蔽特定區域。此功能特別適用於遮蔽表單、簽名、圖片或任何包含敏感資訊的視覺內容,無論內容類型為何。

文字遮蔽與區域遮蔽有何區別?

IronPDF 的文字遮罩功能可鎖定並移除文件中的特定文字字串,而區域遮罩則能根據座標模糊指定區域。區域遮罩特別適用於圖片、簽名或表單欄位等非文字內容,而文字遮罩則非常適合移除特定單字或短語。

實施 PDF 遮蔽功能需要多少步驟?

IronPDF 讓 PDF 遮蔽處理變得簡單,只需 5 個步驟:下載 C# 函式庫、準備您的 PDF 文件、使用 RedactTextOnAllPages 進行文字遮蔽或 RedactRegionsOnAllPages 進行區域遮蔽,最後將遮蔽後的文件儲存為新檔案。

資料遮蔽的處理過程是否永久且安全?

是的,IronPDF 的遮蔽方法會永久移除 PDF 結構中的敏感資訊,而非僅在視覺上進行遮蓋。這確保了被遮蔽的內容無法被恢復或存取,為敏感文件提供真正的安全性與合規性。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。