Chiffrer le fichier PDF
Contents
[
Hide
]
Chiffrer le fichier PDF
Utilisez PdfFileSecurity lorsque vous devez protéger un PDF avec des mots de passe et des règles de privilèges.
Étapes
-
Créez une instance
PdfFileSecurity. -
Liez le PDF source avec
bindPdf. -
Créez un objet
DocumentPrivilegequi correspond aux actions autorisées. -
Appelez la surcharge
encryptFileappropriée pour la taille de clé et l’algorithme dont vous avez besoin. -
Enregistrez le fichier sécurisé et fermez l’objet.
Exemples Java
public static void encryptPdfWithUserOwnerPassword(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
privilege.setAllowPrint(true);
fileSecurity.encryptFile("user_password", "owner_password", privilege, KeySize.x128);
fileSecurity.save(outputFile.toString());
fileSecurity.close();
}
public static void encryptPdfWithPermissions(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
DocumentPrivilege privilege = DocumentPrivilege.getAllowAll();
privilege.setAllowPrint(false);
privilege.setAllowCopy(false);
fileSecurity.encryptFile("user_password", "owner_password", privilege, KeySize.x128);
fileSecurity.save(outputFile.toString());
fileSecurity.close();
}
public static void encryptPdfWithEncryptionAlgorithm(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
privilege.setAllowPrint(true);
fileSecurity.encryptFile("user_password", "owner_password", privilege, KeySize.x256, Algorithm.AES);
fileSecurity.save(outputFile.toString());
fileSecurity.close();
}