Mengatur hak istimewa pada file PDF yang ada
Contents
[
Hide
]
Mengatur hak istimewa pada file PDF yang ada
Gunakan alur kerja ini ketika Anda perlu mengubah apa yang dapat dilakukan pengguna dengan PDF yang ada.
Langkah
- Buat sebuah instans
PdfFileSecurity. - Gabungkan PDF sumber dengan
bindPdf. - Buat sebuah objek
DocumentPrivilegedan mengkonfigurasi tindakan yang diizinkan. - Panggil yang sesuai
setPrivilegeatautrySetPrivilegekelebihan beban. - Simpan hasilnya jika pembaruan berhasil, kemudian tutup objek.
Contoh 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();
}