Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 521
black1011 Avatar asked Oct 05 '18 08:10

black1011


People also ask

How do you password protect a PDF that is already created?

Add a password to Adobe Acrobat (pdf) Open the PDF and choose Tools > Protect > Encrypt > Encrypt with Password. If you receive a prompt, click Yes to change the security. Select Require a Password to Open the Document, then type the password in the corresponding field.

Why can't I password protect a PDF?

1 Correct answer. Go to File - Properties - Security and select "Password Security" under "Security Method". Select your settings, enter your password, and you're done.

How do I password protect a document on my iPad?

Take these steps to password protect a PDF on an iPad. Here's how you can make it happen: Click Select A File to navigate to your file, and choose it. Enter a strong and unique password into the box that appears. Retype the password to confirm it.


1 Answers

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

like image 116
Nikdemm Avatar answered Nov 14 '22 23:11

Nikdemm