Set Privileges on an Existing PDF File
Contents
[
Hide
]
Set privileges on an existing PDF file
Use this workflow when you need to change what users can do with an existing PDF.
Steps
- Create a
PdfFileSecurityinstance. - Bind the source PDF with
bindPdf. - Create a
DocumentPrivilegeobject and configure the allowed actions. - Call the appropriate
setPrivilegeortrySetPrivilegeoverload. - Save the result if the update succeeds, then close the object.
Java examples
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();
}