サーバーから PDF ファイルをダウンロードし、パスワードを追加して、ファイルをローカルに保存するアプリを構築しています。
ファイルにパスワードを設定するのに苦労しています。以下は、パスワードを設定してファイルを保存するために実行する関数です。
- (void)addPassword:(NSString *)password forPDFAtPath:(NSString *)path {
NSData *data = [NSData dataWithContentsOfFile:path];
//Create the pdf document reference
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
//Create the pdf context
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);
CFMutableDictionaryRef ref = CFDictionaryCreateMutable(NULL,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(ref, kCGPDFContextUserPassword, (__bridge CFStringRef)password);
CFDictionarySetValue(ref, kCGPDFContextOwnerPassword, (__bridge CFStringRef)password);
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, ref);
if (CGPDFDocumentGetNumberOfPages(document) > 0) {
//Draw the page onto the new context
page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
CGPDFContextBeginPage(pdfContext, NULL);
CGContextDrawPDFPage(pdfContext, page);
CGPDFContextEndPage(pdfContext);
} else {
NSLog(@"Failed to create the document");
}
CGContextRelease(pdfContext); //Release before writing data to disk.
//Write to disk
[(__bridge NSData *)mutableData writeToFile:path atomically:YES];
//Clean up
CGDataProviderRelease(dataProvider); //Release the data provider
CGDataConsumerRelease(dataConsumer);
CGPDFDocumentRelease(document);
CFRelease(mutableData);}
これにより、パスワードが設定され、ファイルが保存されますが、1 ページしかありません。PDF全体のコピーを作成するにはどうすればよいですか?
私が見る限り、このスクリプトはすべてのページをループして、ページごとに PDF を描画する必要があります。各ページを描画するのではなく、PDF を複製してパスワードを設定する方法はありますか?
前もって感謝します