Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem Unlocking Password Protected PDF documents

Tags:

pdf

iphone

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.

like image 314
Arun Thakkar Avatar asked Feb 04 '10 06:02

Arun Thakkar


People also ask

How do I remove password protection from PDF without password?

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.

Why are my PDF documents locked?

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.


1 Answers

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.

like image 159
Marcus Avatar answered Nov 07 '22 09:11

Marcus