Add a password protection to an existing pdf file using PDFKit iOS
Asked Answered
S

1

7

I wanted to add a password protection to an existing pdf file in my application.

Here's my code:

    if let path = Bundle.main.path(forResource: "pdf_file", ofType: "pdf") {
        let url = URL(fileURLWithPath: path)
        if let pdfDocument = PDFDocument(url: url) {

            pdfDocument.write(to: url, withOptions: [PDFDocumentWriteOption.userPasswordOption : "pwd"])

            pdfView.displayMode = .singlePageContinuous
            pdfView.autoScales = true
            // pdfView.displayDirection = .horizontal
            pdfView.document = pdfDocument
        }
    }

Added the line pdfDocument.write() before viewing the file. I was expecting that the file wouldn't be viewed anymore or it would ask a password first before it can be viewed but i can still view it directly as if that line did not exist.

I tried PSPDFKit before and when i add a password protection to a pdf file, when viewing the file it asks a password first and the file in the application storage is locked/encrypted, but that's not what I'm getting when I used this iOS PDFKit new feature for iOS 11 and later.

Skim answered 5/10, 2018 at 8:20 Comment(0)
S
16

Your problem that you don't encrypt pdfDocument, you write encrypted copy of pdfDocument to disk, if you read this document from disk, it is will be protected. Example:

if let path = Bundle.main.path(forResource: "pdf_file", ofType: "pdf") {
    let url = URL(fileURLWithPath: path)
    if let pdfDocument = PDFDocument(url: url) {
        let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
        let encryptedFileURL = documentDirectory.appendingPathComponent("encrypted_pdf_file")

        // write with password protection
        pdfDocument.write(to: encryptedFileURL, withOptions: [PDFDocumentWriteOption.userPasswordOption : "pwd",
                                                             PDFDocumentWriteOption.ownerPasswordOption : "pwd"])

        // get encrypted pdf
        guard let encryptedPDFDoc = PDFDocument(url: encryptedFileURL) else {
            return
        }

        print(encryptedPDFDoc.isEncrypted) // true
        print(encryptedPDFDoc.isLocked) // true

        pdfView?.displayMode = .singlePageContinuous
        pdfView?.autoScales = true
        pdfView?.displayDirection = .horizontal
        pdfView?.document = encryptedPDFDoc
    }
}

I hope this help

Sachsen answered 5/10, 2018 at 9:49 Comment(5)
Nice, helped a lot. There were 2 mistakes in my code, how i write with password protection, I noticed that .userPassword and .ownerPassword must both be present to lock and encrypt the pdf file. The other one was viewing the pdfDocument, I needed to access the encrypted pdfDocument again and that's the pdfDocument that I need to show. Thanks a lot.Skim
@Skim I’m glad for you)Sachsen
Is it possible to open a pdf with a password?Carmichael
@Carmichael yeah, developer.apple.com/documentation/pdfkit/pdfdocument/…Sachsen
As @Skim mentioned, one of the reasons why the accepted answer works is because compared to the code from the question it also has .ownerPasswordOption which is required for encryption.Guinevere

© 2022 - 2024 — McMap. All rights reserved.