[Solved] How to programmatically enter a password inside a password protected PDF file?


I found the answer I was looking for on this apple site.
Here is how I managed to get it work:

  1. first you transform the filepath string into NSURL(and make CFURL of that) like this:

    let path : NSString = filepath as NSString        
    let thePath = path.stringByExpandingTildeInPath   
    let url = NSURL.fileURLWithPath(thePath)   
    let CFUrl = url as CFURL
    
  2. You import CoreGraphics and CoreFoundation and then make a document like this:

    var document : CGPDFDocumentRef = CGPDFDocumentCreateWithURL(CFUrl)
    
  3. Now you can use the CoreGraphics methods to check if it is unlocked or not and to enter a password like this:

    if CGPDFDocumentIsUnlocked(document) {
        //true if there is NO password   
    }    
    if CGPDFDocumentUnlockWithPassword(document, "password here") {      
        //returns true if the password is correct   
    }
    

3

solved How to programmatically enter a password inside a password protected PDF file?