PDF-Verschlüsselung & -Entschlüsselung

Dieses Codebeispiel zeigt, wie man mit der leistungsstarken C# .NET-PDF-Bibliothek von IronPDF Metadaten ändert, PDF-Dateien schreibgeschützt macht, Berechtigungen konfiguriert und das Passwort für die Dokumentenverschlüsselung ändert.

Zu Beginn importieren Sie ein vorhandenes PDF-Dokument mit der Methode öffnen. Diese Methode kann genutzt werden, um passwortgeschützte Dokumente zu öffnen, indem das Passwort als zweiter Parameter angegeben wird, wodurch eine robuste Handhabung geschützter Dateien gewährleistet wird.

Um neue Metadateninformationen festzulegen, erstellen Sie zunächst ein Wörterbuch und fügen Sie Schlüssel-Wert-Paare für Metadaten wie Autor und Schlüsselwörter hinzu. Nutzen Sie die overrideMetadata -Methode in IronPDF, um die neuen Metadaten effektiv auf das PDF-Dokument anzuwenden.

Als Nächstes entfernen Sie die Passwörter und die Verschlüsselung mit der von IronPDF bereitgestellten Methode removePasswordsAndEncryption und konfigurieren die PDF-Datei als schreibgeschützt, indem Sie mit der Methode makePdfDocumentReadOnly ein neues Passwort festlegen, um die Integrität und Sicherheit des Dokuments zu gewährleisten.

Berechtigungen für das PDF-Dokument werden über ein Objekt namens "permissions" konfiguriert, das angibt, ob bestimmte Aktionen wie Anmerkungen, Inhaltsextraktion, Ausfüllen von Formularen und Drucken erlaubt oder verboten sind. Übergeben Sie das Permissions-Objekt an die Methode setPermission, um die Zugänglichkeitsfunktionen des Dokuments genau zu steuern.

Zum Schluss ändern oder setzen Sie das Dokumentverschlüsselungspasswort auf "mein-passwort" und speichern die geänderte PDF-Datei als "gesichert.pdf". Dies demonstriert die Fähigkeit von IronPDF zur sicheren Dokumentenverwaltung in der Anwendungsentwicklung.

// 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-Verschlüsselungs- und Entschlüsselungscode-Beispiel ansehen

Bereit anzufangen?
Version: 2025.12 gerade veröffentlicht