既存の PDF ファイルへの特権の設定
Contents
[
Hide
]
既存の PDF ファイルへの特権の設定
既存の PDF に対してユーザーが実行できる操作を変更する必要がある場合に、このワークフローを使用してください。
手順
PdfFileSecurityインスタンスを作成してください。- ソース PDF を
bindPdfでバインドしてください。 DocumentPrivilegeオブジェクトを作成し、許可されたアクションを設定してください。- 適切な
setPrivilegeまたはtrySetPrivilegeのオーバーロードを呼び出してください。 - 更新が成功した場合は結果を保存し、その後オブジェクトを閉じてください。
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();
}