我有一个应用程序,可以在其中从视图创建 PDF 文件,然后通过电子邮件发送。我用来生成 PDF 的代码(非常感谢 casilic)是这样的:
- (void)createPDFfromUIView:(UIView*)view saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext]; // this line
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"cherry"];
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}
它运行良好。但是,我想实现保护 PDF 密码的功能。因此,如果他们通过电子邮件将 PDF 发送给自己,则需要输入密码才能查看。我完全不确定如何做到这一点。我添加了:
NSMutableDictionary *myDictionary = CFBridgingRelease(CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
CFDictionarySetValue((__bridge CFMutableDictionaryRef)(myDictionary), kCGPDFContextUserPassword, CFSTR("userpassword"));
CFDictionarySetValue((__bridge CFMutableDictionaryRef)(myDictionary), kCGPDFContextOwnerPassword, CFSTR("ownerpassword"));
我觉得我在正确的轨道上,但我完全不确定从这里该做什么。我已阅读有关密码保护 PDF 的文档: https ://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html#//apple_ref/doc/uid/TP30001066 -CH214-TPXREF101 但这些仍然超出我的想象。
如果有人知道我需要如何修改此代码以密码保护 PDF,那就太棒了。