0
iOSのPDFファイルに画像を挿入するのは簡単です。しかし、パスワードで保護されたPDFに画像を挿入する方法はありますか?パスワードで保護されたPDFファイルに画像を挿入する目的C
iOSのPDFファイルに画像を挿入するのは簡単です。しかし、パスワードで保護されたPDFに画像を挿入する方法はありますか?パスワードで保護されたPDFファイルに画像を挿入する目的C
後で作業して解決しました。
NSString *pdfFilePath1 = [self getPDFFileName];
pdfData = [NSData dataWithContentsOfFile:pdfFilePath1];
NSMutableData* outputPDFData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer =CGDataConsumerCreateWithCFData((CFMutableDataRef)outputPDFData);
CFMutableDictionaryRef attrDictionary = NULL;
attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, @"My Doc");
///Set password
CFDictionarySetValue(attrDictionary, kCGPDFContextUserPassword, CFSTR("userpassword"));
CFDictionarySetValue(attrDictionary, kCGPDFContextOwnerPassword, CFSTR("ownerpassword"));
///
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary);
CFRelease(dataConsumer);
CFRelease(attrDictionary);
CGRect pageRect;
// Draw the old "pdfData" on pdfContext
CFDataRef myPDFData = (__bridge CFDataRef) pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
//Pass Password to PDF
CGPDFDocumentUnlockWithPassword(pdf, "userpassword");
CGDataProviderRelease(provider);
size_t numPages = CGPDFDocumentGetNumberOfPages(pdf);
if (numPages > 0) {
// Loop through each page in the source file
for (size_t i = 1; i <= numPages; i++) {
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, i);
if (pdfPage) {
// Get the page size
CGRect pdfCropTempBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
//CGRect pdfCropBoxRect=CGRectMake(0, 0, _docPanel.frame.size.width, _docPanel.frame.size.height);
if(i==pageNumber)
{
int offset=1;
int offset_y=editView.frame.size.height;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
offset=10;
offset_y=0;
}
// Copy the page from the source file to the context
CGContextBeginPage(pdfContext, &pdfCropTempBoxRect);
CGContextDrawPDFPage(pdfContext, pdfPage);
CGFloat contentHeight = self.docPanel.scrollView.contentSize.height;
self.pdfPageHeight = contentHeight/self.pdfPageCount;
// also calculate what half the screen height is. no sense in doing this multiple times.
self.halfScreenHeight = (self.docPanel.frame.size.height/2);
float imgX=(editView.frame.origin.x/_docPanel.frame.size.width)*(pdfCropTempBoxRect.size.width);
//float imgY=(1-(verticalContentOffset-self.pdfPageHeight*(i-1)+editView.frame.origin.y+editView.frame.size.height)/self.pdfPageHeight)*pdfCropTempBoxRect.size.height-(i-1)*offset+offset_y;
float imgY=(1-(verticalContentOffset-self.pdfPageHeight*(i-1)+editView.frame.origin.y+editView.frame.size.height)/self.pdfPageHeight)*pdfCropTempBoxRect.size.height+offset_y;
// imgY = (pageNumber*self.pdfPageHeight)-imgY;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
imgY = imgY - (editView.frame.size.height/2);
}
pageRect = CGRectMake(imgX,imgY,editView.frame.size.width-2 ,editView.frame.size.height-2);
CGImageRef pageImage = [img CGImage];
CGContextDrawImage(pdfContext, pageRect, pageImage);
//CGPDFContextEndPage(pdfContext);
CGPDFContextEndPage(pdfContext);
}
else
{
CGContextBeginPage(pdfContext, &pdfCropTempBoxRect);
CGContextDrawPDFPage(pdfContext, pdfPage);
CGPDFContextEndPage(pdfContext);
}
}
}
}
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);
NSString *pdfFilePath = [self getPDFFileName];
[outputPDFData writeToFile:pdfFilePath atomically:YES];