PDF 加密與解密
本代码示例演示了如何使用 IronPDF 强大的 C# .NET PDF 库修改元数据、使 PDF 为只读、配置权限以及更改文档加密密码。
首先,使用 open 方法匯入現有的 PDF 文件。 可以透過將密碼指定為第二個參數來使用此方法開啟受密碼保護的文檔,從而對受保護的文件進行穩健的處理。
要設定新的元資料訊息,首先要建立一個字典,並添加元資料的鍵值對,例如作者和關鍵字。 利用 IronPDF 中的overrideMetadata方法,可以有效地將新的元資料套用到 PDF 文件中。
接下來,使用 IronPDF 提供的 removePasswordsAndEncryption 方法移除密碼和加密,並使用 makePdfDocumentReadOnly 方法設定新密碼,將 PDF 設定為唯讀,確保文件的完整性和安全性。
PDF 文件的權限是使用名為"權限"的物件來設定的,該物件會指定是否允許或禁止某些動作,例如註解、內容擷取、表格填寫和列印。 將權限物件傳送到 setPermission 方法,以精確控制文件的可存取性功能。
最後,將文件加密密碼更改為"my-password",並將修改後的 PDF 儲存為"secured.pdf",以此展示 IronPDF 在應用程式開發中安全文件管理的功能。
// Import the necessary namespace.
using IronPdf;
class PdfExample
{
static void Main()
{
// Open an existing PDF document. If the document is password protected, provide the password.
var pdf = IronPdf.PdfDocument.FromFile("existing.pdf", "document-password");
// Initialize a dictionary to store metadata key-value pairs.
var newMetadata = new Dictionary<string, string>
{
{ "Author", "New Author" },
{ "Keywords", "PDF, Documentation, Example" }
};
// Apply the new metadata to the PDF document.
pdf.OverrideMetadata(newMetadata);
// Remove any existing passwords and encryption.
pdf.RemovePasswordsAndEncryption();
// Make the PDF document read-only by setting a read-only password.
pdf.MakePdfDocumentReadOnly("read-only-password");
// Define and set the permissions for the PDF document.
var permissions = new PdfPermissions
{
AllowAnnotations = false,
AllowContentExtraction = false,
AllowFormFilling = true,
AllowPrinting = false,
AllowDocumentAssembly = false
};
pdf.SetPermissions(permissions);
// Change the document encryption password.
pdf.SaveAs("secured.pdf", "my-password"); // Save the modified PDF with a new security password.
}
}// Import the necessary namespace.
using IronPdf;
class PdfExample
{
static void Main()
{
// Open an existing PDF document. If the document is password protected, provide the password.
var pdf = IronPdf.PdfDocument.FromFile("existing.pdf", "document-password");
// Initialize a dictionary to store metadata key-value pairs.
var newMetadata = new Dictionary<string, string>
{
{ "Author", "New Author" },
{ "Keywords", "PDF, Documentation, Example" }
};
// Apply the new metadata to the PDF document.
pdf.OverrideMetadata(newMetadata);
// Remove any existing passwords and encryption.
pdf.RemovePasswordsAndEncryption();
// Make the PDF document read-only by setting a read-only password.
pdf.MakePdfDocumentReadOnly("read-only-password");
// Define and set the permissions for the PDF document.
var permissions = new PdfPermissions
{
AllowAnnotations = false,
AllowContentExtraction = false,
AllowFormFilling = true,
AllowPrinting = false,
AllowDocumentAssembly = false
};
pdf.SetPermissions(permissions);
// Change the document encryption password.
pdf.SaveAs("secured.pdf", "my-password"); // Save the modified PDF with a new security password.
}
}' Import the necessary namespace.
Imports IronPdf
Friend Class PdfExample
Shared Sub Main()
' Open an existing PDF document. If the document is password protected, provide the password.
Dim pdf = IronPdf.PdfDocument.FromFile("existing.pdf", "document-password")
' Initialize a dictionary to store metadata key-value pairs.
Dim newMetadata = New Dictionary(Of String, String) From {
{"Author", "New Author"},
{"Keywords", "PDF, Documentation, Example"}
}
' Apply the new metadata to the PDF document.
pdf.OverrideMetadata(newMetadata)
' Remove any existing passwords and encryption.
pdf.RemovePasswordsAndEncryption()
' Make the PDF document read-only by setting a read-only password.
pdf.MakePdfDocumentReadOnly("read-only-password")
' Define and set the permissions for the PDF document.
Dim permissions = New PdfPermissions With {
.AllowAnnotations = False,
.AllowContentExtraction = False,
.AllowFormFilling = True,
.AllowPrinting = False,
.AllowDocumentAssembly = False
}
pdf.SetPermissions(permissions)
' Change the document encryption password.
pdf.SaveAs("secured.pdf", "my-password") ' Save the modified PDF with a new security password.
End Sub
End Class




