Skip to content

Instantly share code, notes, and snippets.

@benbahrenburg
Last active January 1, 2017 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbahrenburg/8bdd9aaa087402a0a4fd5d65a04c6c26 to your computer and use it in GitHub Desktop.
Save benbahrenburg/8bdd9aaa087402a0a4fd5d65a04c6c26 to your computer and use it in GitHub Desktop.
Unlock a PDF document
if let pdf = unlock(data: pdfData, password: "Hello World") {
print("You now have an unlocked CGPDFDocument")
}
func unlock(data: Data, password: String) -> CGPDFDocument? {
if let pdf = CGPDFDocument(CGDataProvider(data: data as CFData)!) {
guard pdf.isEncrypted == true else { return pdf }
guard pdf.unlockWithPassword("") == false else { return pdf }
if let cPasswordString = password.cString(using: String.Encoding.utf8) {
if pdf.unlockWithPassword(cPasswordString) {
return pdf
}
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment