I need help unlocking Encrypted PDF Documents.
I have 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.
I have forgotten anything? Is there anything wrong??
Regards, Arun Thakkar.
In the "Security" tab, select "No Security" in the "Security Method" drop-down menu to remove the password. Then you can save the PDF without password protection.
Copyright ProtectionProtecting the information in a document from copyright infringement is one reason why you may choose to lock a PDF file. Locking the file for this reason usually involves adding password protection to prevent unauthorized users from copying or printing the document.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With