I need help unlocking Encrypted PDF Documents.
I have tried the following without success.
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL, documentsDirectory, kCFURLPOSIXPathStyle, 0); //1
pdf = CGPDFDocumentCreateWithURL((C开发者_Python百科FURLRef)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.
I have forgotten anything? Is there anything wrong??
Regards, Arun Thakkar.
I assume that "theTextField" is a UITextField, and you're accessing its text property. The problem is that that property is an NSString (an object), but you need a plain C string to unlock the PDF.
Do this instead:
const char *key = [theTextField.text UTF8String];
BOOL success = CGPDFDocumentUnlockWithPassword(pdf, key);
You were actually trying to unlock the PDF using the string's pointer, something like 0x4d38340, translated into whatever characters are produced by ASCII (or Unicode, not sure) values 4d, 38 and 34 in this case.
精彩评论