问题解锁受密码保护的PDF文档

Aru*_*kar 5 pdf iphone

我需要帮助解锁加密的PDF文档.

我试过以下但没有成功.

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); 
}
Run Code Online (Sandbox Code Playgroud)

我确保密码正确但解锁功能仍然返回False.

我忘记了什么?有什么不对的吗??

此致,Arun Thakkar.

小智 9

我假设"theTextField"是一个UITextField,并且您正在访问其text属性.问题是该属性是一个NSString(一个对象),但你需要一个普通的C字符串来解锁PDF.

改为:

const char *key = [theTextField.text UTF8String];
BOOL success = CGPDFDocumentUnlockWithPassword(pdf, key);
Run Code Online (Sandbox Code Playgroud)

您实际上是尝试使用字符串的指针解锁PDF,类似于0x4d38340,在这种情况下转换为由ASCII(或Unicode,不确定)值4d,38和34生成的任何字符.