PDF 加密與解密

本代码示例演示了如何使用 IronPDF 强大的 C# .NET PDF 库修改元数据、使 PDF 为只读、配置权限以及更改文档加密密码。

首先,使用開啟方法匯入現有的 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
$vbLabelText   $csharpLabel

探索 PDF 加解密程式碼範例

準備好開始了嗎?
版本: 2025.11 剛剛發布