Mengatur hak istimewa pada file PDF yang ada

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

  1. Buat sebuah instans PdfFileSecurity.
  2. Gabungkan PDF sumber dengan bindPdf.
  3. Buat sebuah objek DocumentPrivilege dan mengkonfigurasi tindakan yang diizinkan.
  4. Panggil yang sesuai setPrivilege atau trySetPrivilege kelebihan beban.
  5. 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();
}