Definir privilégios em um arquivo PDF existente
Contents
[
Hide
]
Definir privilégios em um arquivo PDF existente
Use este fluxo de trabalho quando precisar alterar o que os usuários podem fazer com um PDF existente.
Etapas
- Crie uma instância de
PdfFileSecurity. - Vincule o PDF de origem com
bindPdf. - Crie um objeto
DocumentPrivilegee configure as ações permitidas. - Chame o apropriado
setPrivilegeoutrySetPrivilegesobrecarga. - Salve o resultado se a atualização for bem-sucedida, e então feche o objeto.
Exemplos de Java
public static void setPdfPrivilegesWithoutPasswords(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
privilege.setAllowPrint(true);
fileSecurity.setPrivilege(privilege);
fileSecurity.save(outputFile.toString());
fileSecurity.close();
}
public static void setPdfPrivilegesWithPasswords(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
privilege.setAllowPrint(true);
privilege.setAllowCopy(false);
fileSecurity.setPrivilege("user_password", "owner_password", privilege);
fileSecurity.save(outputFile.toString());
fileSecurity.close();
}
public static void trySetPdfPrivilegesWithoutException(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
privilege.setAllowPrint(true);
if (fileSecurity.trySetPrivilege("user_password", "owner_password", privilege)) {
fileSecurity.save(outputFile.toString());
} else {
System.out.println("Setting privileges failed. Check passwords or document state.");
}
fileSecurity.close();
}