Problem Unlocking Password-Protected PDFs

I need help unlocking encrypted PDFs.

I tried the following without success.

CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL, documentsDirectory,  kCFURLPOSIXPathStyle, 0); //1
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
BOOL encrypted = CGPDFDocumentIsEncrypted(pdf);
if (encrypted) {

// Try 1:

    const char *str = (char *)theTextField.text; 
    BOOL _unlock = CGPDFDocumentUnlockWithPassword(pdf,str);

//Try 2:

    NSString *str1 = @"password";
    BOOL _unlock1 = CGPDFDocumentUnlockWithPassword(pdf,str1); 
}

I made sure the password is correct, but the unlock function still returns False.

Did I forget something? Something is wrong??

Regards, Arun Thakkar

+5
source share
1 answer

I assume that "theTextField" is a UITextField and you are accessing its text property. The problem is that this property is an NSString (object), but a simple C string is required to unlock the PDF.

Do this instead:

const char *key = [theTextField.text UTF8String];
BOOL success = CGPDFDocumentUnlockWithPassword(pdf, key);

PDF , - 0x4d38340, , 4d, 38 34 ASCII ( Unicode, ).

+9

All Articles