How to Decrypt a PDF using PDFKit in Swift?

801 Views Asked by At

I have a password protected PDF file and I need to Decrypt it using PDFKit or some other way, When I use unlock with password, it is unlocked but it is still encrypted could anyone help me with this code:

func handlePDF(url: URL, password: String) {                      
    print(pdfDocument.isEncrypted, pdfDocument.isLocked) // true, true
                            
    if let pdfDocument = PDFDocument(url: url) {
        if pdfDocument.isEncrypted {
            if pdfDocument.unlock(withPassword: password) {
                print(pdfDocument.isEncrypted, pdfDocument.isLocked) //true , false
            }
        }
    }
}
1

There are 1 best solutions below

11
matt On

As the documentation explains, your result is expected and correct:

 isEncrypted: Bool { get }

true if the document is encrypted, whether it is locked or unlocked; false otherwise.

(My italics.) So don't worry, be happy.