Decrypt PDF File
Contents
[
Hide
]
Decrypt PDF file
Use this workflow when you have the owner password and need to remove security from a PDF.
Steps
- Create a
PdfFileSecurityinstance. - Bind the encrypted PDF with
bindPdf. - Call
decryptFileortryDecryptFilewith the owner password. - Save the output if decryption succeeds.
- Close the security object.
Java examples
public static void decryptPdfWithOwnerPassword(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
fileSecurity.decryptFile("owner_password");
fileSecurity.save(outputFile.toString());
fileSecurity.close();
}
public static void tryDecryptPdfWithoutException(Path inputFile, Path outputFile) {
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.bindPdf(inputFile.toString());
if (fileSecurity.tryDecryptFile("owner_password")) {
fileSecurity.save(outputFile.toString());
} else {
System.out.println("Decryption failed. Check password or document security.");
}
fileSecurity.close();
}