Chiffrement et déchiffrement PDF

Cet exemple de code démontre comment modifier les métadonnées, rendre le PDF en lecture seule, configurer les autorisations, et changer le mot de passe de chiffrement du document en utilisant la puissante bibliothèque PDF C# .NET de IronPDF.

Pour commencer, importez un document PDF existant en utilisant la méthode open. Cette méthode peut être utilisée pour ouvrir des documents protégés par un mot de passe en spécifiant le mot de passe comme deuxième paramètre, offrant une gestion robuste des fichiers sécurisés.

Pour définir de nouvelles informations de métadonnées, commencez par créer un dictionnaire et ajoutez des paires clé-valeur pour les métadonnées, telles que l'auteur et les mots-clés. Utilisez la méthode overrideMetadata dans IronPDF pour appliquer efficacement les nouvelles métadonnées au document PDF.

Ensuite, supprimez les mots de passe et le chiffrement en utilisant la méthode removePasswordsAndEncryption fournie par IronPDF, et configurez le PDF pour qu'il soit en lecture seule en définissant un nouveau mot de passe avec la méthode makePdfDocumentReadOnly, assurant ainsi l'intégrité et la sécurité du document.

Les autorisations pour le document PDF sont configurées à l'aide d'un objet nommé "permissions", qui spécifie si certaines actions, telles que les annotations, l'extraction de contenu, le remplissage de formulaires, et l'impression, sont autorisées ou non. Passez l'objet permissions à la méthode setPermission pour contrôler précisément les fonctionnalités d'accessibilité du document.

Enfin, changez ou définissez le mot de passe de chiffrement du document par "my-password", et enregistrez le PDF modifié sous le nom "secured.pdf", montrant la capacité d'IronPDF à gérer en toute sécurité les documents dans le développement d'applications.

// 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

Explorez l'exemple de code de chiffrement et déchiffrement PDF

Prêt à commencer?
Version : 2025.11 vient de sortir