Establecer privilegios en un archivo PDF existente
Contents
[
Hide
]
Establecer privilegios en un archivo PDF existente
Utilice este flujo de trabajo cuando necesite cambiar lo que los usuarios pueden hacer con un PDF existente.
Pasos
- Cree una instancia de
PdfFileSecurity. - Enlace el PDF de origen con
bindPdf. - Cree un objeto
DocumentPrivilegey configure las acciones permitidas. - Llame al apropiado
setPrivilegeotrySetPrivilegesobrecarga. - Guarde el resultado si la actualización tiene éxito, luego cierre el objeto.
Ejemplos 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();
}