기존 PDF 파일에 대한 권한 설정

기존 PDF 파일에 대한 권한 설정

사용자가 기존 PDF로 수행할 수 있는 작업을 변경해야 하는 경우 이 작업 과정을 사용하세요.

단계

  1. PdfFileSecurity 인스턴스를 생성하세요.
  2. bindPdf을 사용하여 소스 PDF를 바인딩하세요.
  3. DocumentPrivilege 개체를 생성하고 허용되는 작업을 구성하세요.
  4. 적절한 setPrivilege 또는 trySetPrivilege 오버로드를 호출하세요.
  5. 업데이트가 성공하면 결과를 저장한 다음 개체를 닫으세요.

자바 예제

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();
}