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 刚刚发布