How To Secure PDF Files in C#

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

對於處理私人、敏感或機密 PDF 文件的開發人員來說,PDF 安全性是一個至關重要的方面。 IronPDF 的設計是為了賦予開發人員一個可以輕鬆處理 PDF 安全性的庫,且無需冗長的學習曲線。 使用 IronPDF,您可以輕鬆簽署 PDF 文件以確保真實性,創建自定義權限來控制人們如何與您的文件互動,填寫 PDF 表單等等,所有這些都以一種實用、高效的方式進行。

因此,如果您正在尋找一種可以處理從 PDF 簽署到編輯 PDF 表單的一切工具,那麼 IronPDF 就是您所需要的工具。 憑藉簡易實現的 API,您可以在短時間內在 C# 應用程序中運行 IronPDF。它使您能夠創建僅需幾行代碼即可處理 PDF 安全性的可讀代碼,消除了複雜難懂的代碼文件的需求,減輕了負擔。

在本教程中,我們將首先介紹 IronPDF 的全面 PDF 安全功能集。 接下來,我們將探索一些展示這些工具運作的代碼示例。 在今天的學習結束時,您將能夠輕鬆使用 IronPDF 創建安全的 PDF 文件。 那麼,讓我們開始吧!

快速入門:使用數位簽名保護您的 PDF

快速入門使用 IronPDF,通過添加數位簽名來增強您的 PDF 安全性。 這個簡單的示例展示了如何加載 PDF,使用證書應用數位簽名,並保存已保護的文檔。 只需極少代碼,您就可以確保 PDF 的完整性和真實性,使其防篡改且對敏感信息安全。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = IronPdf.PdfDocument.FromFile("input.pdf");
    pdf.SignWithFile("certificate.pfx", "password");
    pdf.SaveAs("secured.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

目錄

NuGet 用 NuGet 安裝

PM >  Install-Package IronPdf

NuGet 查看 https://www.nuget.org/packages/IronPdf 以快速安裝。超過 1000 萬次下載,它正在用 C# 改變 PDF 開發。 您還可以下載 DLLWindows 安裝程序

確保真實性

通常在處理 PDF 時,開發人員和公司需要確保文件的真實性。 確保 PDF 的真實性有多種原因,從法律和規範標準的合規性到長期存檔,甚至是數位取證等情況。 無論原因如何,IronPDF 提供了一種無縫的方法來將數位簽名應用於 PDF 文件並驗證修訂歷史。

簽署 PDF

IronPDF 簡化了程序化簽署 PDF 文件的過程,提供了簡明的方法和多種適合您特定需求的方法。 這些方法設計得直觀易用,使您能夠放心地簽署 PDF。

  • 使用證書對 PDF 進行數位簽名。
  • 在現有 PDF 上添加圖形簽名。
  • 將證書圖片添加到 PDF。
  • 在 PDF 上添加留白簽名欄供查看者簽名。

在這個例子中,讓我們看看如何使用數位簽名簽署 PDF。

:path=/static-assets/pdf/content-code-examples/how-to/signing-X509Certificate2-with-privatekey.cs
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>foo</h1>");

// Create X509Certificate2 object with X509KeyStorageFlags set to Exportable
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

// Create PdfSignature object
var sig = new PdfSignature(cert);

// Sign PDF document
pdf.Sign(sig);

pdf.SaveAs("signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates

Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>foo</h1>")

' Create X509Certificate2 object with X509KeyStorageFlags set to Exportable
Private cert As New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable)

' Create PdfSignature object
Private sig = New PdfSignature(cert)

' Sign PDF document
pdf.Sign(sig)

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

請訪問 操作指南以了解更多 IronPDF 的簽名功能並查看其他方法的實際操作。

設置和編輯元數據

當我們討論 PDF 文件的元數據時,我們指的是關於 PDF 自身的重要信息,如作者、創建日期、關鍵字、版權信息等。 這些元數據的安全性極為重要,因為其中可能包含需要保護以免被未授權個體利用或揭露的敏感信息。

使用 IronPDF,您可以輕鬆編輯您的 PDF 元數據,以確保其不包含任何敏感信息。 另一種方法可以是加密您的 PDF 以防止未授權訪問; 不過,我們將在這篇文章的後面進一步探討 PDF 加密。 除了基本的安全考量外,利用 PDF 元數據還有很多好處。 這樣做可以提高資料庫搜索性、增加互聯網搜索性,並啟發使用和互動 PDF 文件的新方式。

通過設置自定義元數據,如關鍵字字段,您可以輕鬆告知讀者在您的 PDF 中可以預期找到哪些信息,並確保其出現在相關搜尋中。

:path=/static-assets/pdf/content-code-examples/how-to/metadata-set-edit.cs
using IronPdf;
using System;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Metadata</h1>");

// Access the MetaData class and set the pre-defined metadata properties.
pdf.MetaData.Author = "Iron Software";
pdf.MetaData.CreationDate = DateTime.Today;
pdf.MetaData.Creator = "IronPDF";
pdf.MetaData.Keywords = "ironsoftware,ironpdf,pdf";
pdf.MetaData.ModifiedDate = DateTime.Now;
pdf.MetaData.Producer = "IronPDF";
pdf.MetaData.Subject = "Metadata Tutorial";
pdf.MetaData.Title = "IronPDF Metadata Tutorial";

pdf.SaveAs("pdf-with-metadata.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

有關此代碼片段的更詳細說明及探索其附加功能,請參閱我們全面的 操作指南

編輯和簽署修訂歷史

在處理 PDF 文件時,修訂歷史是一個重要功能,它允許您恢復文檔的先前版本,並隨時間跟踪和管理對 PDF 文件所做的更改。使用 IronPDF,您可以輕鬆管理 PDF 修訂歷史,查看在協作環境中誰進行了更改,並簽署 PDF 的修訂歷史以確保其真實性。

:path=/static-assets/pdf/content-code-examples/how-to/signing-revision.cs
using IronPdf;
using IronPdf.Rendering;

// Import PDF and enable TrackChanges
PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf", TrackChanges: ChangeTrackingModes.EnableChangeTracking);
// ... various edits ...
pdf.SignWithFile("/assets/IronSignature.p12", "password", null, IronPdf.Signing.SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed);

PdfDocument pdfWithRevision = pdf.SaveAsRevision();

pdfWithRevision.SaveAs("annual_census_2.pdf");
Imports IronPdf
Imports IronPdf.Rendering

' Import PDF and enable TrackChanges
Private pdf As PdfDocument = PdfDocument.FromFile("annual_census.pdf", TrackChanges:= ChangeTrackingModes.EnableChangeTracking)
' ... various edits ...
pdf.SignWithFile("/assets/IronSignature.p12", "password", Nothing, IronPdf.Signing.SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed)

Dim pdfWithRevision As PdfDocument = pdf.SaveAsRevision()

pdfWithRevision.SaveAs("annual_census_2.pdf")
$vbLabelText   $csharpLabel

有關此代碼片段的更詳細說明及探索其附加功能,請參閱我們全面的 操作指南

PDF 表單管理

從應用程序到調查,您可能有多種理由需要處理 PDF 表單。 IronPDF 提供了全面的 PDF 表單處理功能,涵蓋從創建新表單到編輯 PDF 表單,以及將表單字段扁平化以防止進一步編輯的所有內容。 使用 IronPDF,您可以很好地處理所有表單管理需求。

創建 PDF 表單

使用 IronPDF 易於學習的 API,添加自定義表單到您的 PDF 文件中變得輕而易舉。 我們的表單工具支持廣泛的表單元素,包括單選按鈕、複選框、文本區域、輸入字段等。 對於希望創建需要填寫者簽名的表單的人來說,IronPDF 支持在表單中添加留白簽名欄。

通過使用 IronPDF 創建動態表單,您可以確保這些表單滿足您的需求並完全控制整個設計過程。

:path=/static-assets/pdf/content-code-examples/how-to/create-forms-input-textarea.cs
using IronPdf;

// Input and Text Area forms HTML
string FormHtml = @"
<html>
    <body>
        <h2>Editable PDF Form</h2>
        <form>
            First name: <br> <input type='text' name='firstname' value=''> <br>
            Last name: <br> <input type='text' name='lastname' value=''> <br>
            Address: <br> <textarea name='address' rows='4' cols='50'></textarea>
        </form>
    </body>
</html>
";

// Instantiate Renderer
ChromePdfRenderer Renderer = new ChromePdfRenderer();
Renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

Renderer.RenderHtmlAsPdf(FormHtml).SaveAs("textAreaAndInputForm.pdf");
Imports IronPdf

' Input and Text Area forms HTML
Private FormHtml As String = "
<html>
    <body>
        <h2>Editable PDF Form</h2>
        <form>
            First name: <br> <input type='text' name='firstname' value=''> <br>
            Last name: <br> <input type='text' name='lastname' value=''> <br>
            Address: <br> <textarea name='address' rows='4' cols='50'></textarea>
        </form>
    </body>
</html>
"

' Instantiate Renderer
Private Renderer As New ChromePdfRenderer()
Renderer.RenderingOptions.CreatePdfFormsFromHtml = True

Renderer.RenderHtmlAsPdf(FormHtml).SaveAs("textAreaAndInputForm.pdf")
$vbLabelText   $csharpLabel

如果您想了解更多關於如何使用 IronPDF 創建自定義 PDF 表單以及它支持哪些元素的資訊,請檢查本指南

填寫和編輯 PDF 表單

使用 IronPDF,您可以通過程序化填寫包含動態數據的表單來自動化 PDF 簽署過程。 無論是來自用戶輸入、已連接的數據庫或任何其他方法,IronPDF 都可以輕鬆完成。 您還可以使用它來編輯預先存在的表單以更好地滿足您的需求。

:path=/static-assets/pdf/content-code-examples/how-to/edit-forms-input-textarea.cs
using IronPdf;

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

// Set text input form values
pdf.Form.FindFormField("firstname").Value = "John";
pdf.Form.FindFormField("lastname").Value = "Smith";

// Set text area form values
pdf.Form.FindFormField("address").Value = "Iron Software LLC\r\n205 N. Michigan Ave.";

pdf.SaveAs("textAreaAndInputFormEdited.pdf");
Imports Microsoft.VisualBasic
Imports IronPdf

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

' Set text input form values
pdf.Form.FindFormField("firstname").Value = "John"
pdf.Form.FindFormField("lastname").Value = "Smith"

' Set text area form values
pdf.Form.FindFormField("address").Value = "Iron Software LLC" & vbCrLf & "205 N. Michigan Ave."

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

有關此代碼片段的更詳細說明及探索其附加功能,請參閱我們全面的 操作指南

文件安全

確保您的 PDF 文件安全是處理私人 PDF 文件(如包含敏感或機密信息的文件)時的基本步驟。 通過實施 PDF 安全功能,例如加密和權限設置,您可以確保只有授權個體才能訪問 PDF 文件。

清理 PDF 文件

IronPDF 使用其“清理器”類使清理 PDF 文件的過程變得輕鬆。 此功能可刪除可能包含您不想分享的敏感或私人信息的任何隱藏數據和元數據。 為了清理 PDF 文件,IronPDF 會首先將該 PDF 文件轉換為圖像文件類型,然後再轉換回 PDF 格式,現在已清理掉任何私密數據。

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

// Import PDF document
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Sanitize with Bitmap
PdfDocument sanitizeWithBitmap = Cleaner.SanitizeWithBitmap(pdf);

// Sanitize with SVG
PdfDocument sanitizeWithSvg = Cleaner.SanitizeWithSvg(pdf);

// Export PDFs
sanitizeWithBitmap.SaveAs("sanitizeWithBitmap.pdf");
sanitizeWithSvg.SaveAs("sanitizeWithSvg.pdf");
Imports IronPdf

' Import PDF document
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Sanitize with Bitmap
Private sanitizeWithBitmap As PdfDocument = Cleaner.SanitizeWithBitmap(pdf)

' Sanitize with SVG
Private sanitizeWithSvg As PdfDocument = Cleaner.SanitizeWithSvg(pdf)

' Export PDFs
sanitizeWithBitmap.SaveAs("sanitizeWithBitmap.pdf")
sanitizeWithSvg.SaveAs("sanitizeWithSvg.pdf")
$vbLabelText   $csharpLabel

想要了解更多有關 IronPDF 清理方法的信息嗎? 請一定要查看這一主題的 操作指南

設置 PDF 密碼和權限

通過為您的 PDF 設置密碼來限制訪問並自定義用戶交互的權限,您可以確保只有授權個體才能查看您的 PDF 文件。

IronPDF 的加密過程使用 128 位加密對您的 PDF 文件進行加密,使您可以完全控制權限,決定用戶是否可以編輯、打印、註釋或對 PDF 執行其他任務。

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-add-password.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Secret Information:</h1> Hello World");

// Password to edit the pdf
pdf.SecuritySettings.OwnerPassword = "123password";

// Password to open the pdf
pdf.SecuritySettings.UserPassword = "password123";

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

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Secret Information:</h1> Hello World")

' Password to edit the pdf
pdf.SecuritySettings.OwnerPassword = "123password"

' Password to open the pdf
pdf.SecuritySettings.UserPassword = "password123"

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

通過遵循我們今天所檢查的各種方法,您可以僅使用幾行代碼創建安全的 PDF 文件,使該過程變得簡單。

有關此代碼片段的詳細說明及探索其附加功能,請參閱我們全面的 操作指南

結論

總而言之,IronPDF 在增強 PDF 文件的簽署和安全方面發揮了至關重要的作用。 通過利用其清理功能,用戶可以輕鬆刪除敏感元數據,確保僅共享必要信息。 此外,IronPDF 的強大加密能力使得強密碼保護和可定制的權限的實施成為可能,使文件所有者能夠控制對其 PDF 的訪問權和交互方式。 這種安全措施的組合保證了機密信息的保護,同時也促進了重要文件的安全共享和簽署。 只需幾行代碼,IronPDF 就簡化了創建安全 PDF 的過程,使其成為任何處理敏感內容的人都不可或缺的工具,並在過程中樹立了易用性和信心。

若希望提出功能請求或對 IronPDF 或授權有任何一般性問題,請聯繫我們的支持團隊。 我們將很樂意幫助您。

常見問題解答

如何使用 C# 保護 PDF 檔案?

若要在 C# 中保護 PDF 文件,您可以利用 IronPDF 的全面安全功能,包括加密和密碼保護,以保護您的 PDF 文件。

什麼是PDF檔案中的數位簽章?

PDF 文件的數位簽名是指在文件中添加數位簽名,以驗證其真實性和完整性。 IronPDF 支援使用 C# 增加數位簽名,以確保文件的安全性和可信度。

為什麼PDF安全性很重要?

PDF 安全性對於保護敏感資訊免遭未經授權的存取以及確保文件不被篡改至關重要。使用 IronPDF 等工具,您可以實施強大的安全措施,例如加密和數位簽章。

我可以使用 IronPDF 為 PDF 檔案添加密碼保護嗎?

是的,IronPDF 允許您使用 C# 為 PDF 檔案新增密碼保護,確保只有授權使用者才能開啟和檢視文件。

IronPDF支援哪些類型的加密?

IronPDF 支援多種加密標準,包括 128 位元和 256 位元 AES 加密,為您的 PDF 文件提供強大的保護。

如何驗證PDF文件的真實性?

您可以使用數位簽名來驗證 PDF 文件的真實性,IronPDF 可以使用 C# 以程式設計方式新增數位簽名,以確認文件的來源和完整性。

是否可以限制對PDF文件的編輯?

是的,使用 IronPDF,您可以透過設定權限來限制 PDF 檔案的編輯,防止未經授權的修改,同時允許檢視或列印等其他操作。

在 C# 中使用 IronPDF 進行 PDF 安全保護有什麼好處?

IronPDF 提供了一個易於使用的 API,用於實現高級 PDF 安全功能,例如加密、密碼保護和數位簽名,使其成為使用 C# 的開發人員的理想選擇。

如何確保PDF文件的完整性?

透過使用 IronPDF 新增數位簽名,可以確保 PDF 文件的完整性,從而驗證文件自簽名以來是否已更改。

IronPDF 能否與現有的 C# 應用程式整合以實現 PDF 安全?

是的,IronPDF 可以無縫整合到現有的 C# 應用程式中,以提供增強的 PDF 安全功能,包括加密、密碼保護和數位簽章。

IronPDF 是否完全相容於新的 .NET 10 平台?這是否會影響其 PDF 安全功能?

是的,IronPDF 與 .NET 10 完全相容,包括其安全性相關功能,例如數位簽章、加密和密碼保護。它可在所有支援平台上的 .NET 10 專案中開箱即用,無需任何特殊設定。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/tutorials/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Tutorials.php
Line: 29
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/tutorials/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Tutorials.php
Line: 29
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/tutorials/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Tutorials.php
Line: 29
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,133,208 | 版本: 2025.11 剛剛發布