تعيين الصلاحيات على ملف PDF موجود

تعيين الصلاحيات على ملف PDF موجود

استخدم هذا سير العمل عندما تحتاج إلى تغيير ما يمكن للمستخدمين القيام به مع ملف PDF موجود

الخطوات

  1. أنشئ مثيلًا من PdfFileSecurity.
  2. اربط ملف PDF المصدر بـ bindPdf.
  3. أنشئ الكائن DocumentPrivilege واضبط الإجراءات المسموح بها.
  4. استدعِ بالملائم setPrivilege أو trySetPrivilege تحميل زائد.
  5. احفظ النتيجة إذا نجح التحديث، ثم أغلق الكائن.

أمثلة 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();
}