Alterar senha do arquivo PDF
Contents
[
Hide
]
Alterar senha do arquivo PDF
Use PdfFileSecurity quando precisar rotacionar credenciais em um PDF já protegido.
Etapas
- Crie uma instância de
PdfFileSecurity. - Vincule o PDF protegido com
bindPdf. - Chame o apropriado
changePasswordsobrecarga, dependendo de se você também deseja redefinir privilégios e tamanho da chave. - Salve o arquivo atualizado e feche o objeto de segurança.
Exemplos em Java
public static void changeUserAndOwnerPassword(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
fileSecurity.changePassword("owner_password", "new_user_password", "new_owner_password");
fileSecurity.save(outputFile.toString());
fileSecurity.close();
}
public static void changePasswordAndResetSecurity(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
privilege.setAllowPrint(true);
fileSecurity.changePassword("owner_password", "new_user_password", "new_owner_password", privilege, KeySize.x128);
fileSecurity.save(outputFile.toString());
fileSecurity.close();
}
public static void tryChangePasswordWithoutException(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
if (fileSecurity.tryChangePassword("owner_password", "new_user_password", "new_owner_password")) {
fileSecurity.save(outputFile.toString());
} else {
System.out.println("Password change failed. Check owner password or document security.");
}
fileSecurity.close();
}