Ios 在受密码保护的PDF文件中插入图像目标C
在iOS的PDF文件中插入图像很容易。但有没有办法在受密码保护的PDF中插入图像。后来,我处理并解决了它Ios 在受密码保护的PDF文件中插入图像目标C,ios,objective-c,iphone,pdf,pdf-generation,Ios,Objective C,Iphone,Pdf,Pdf Generation,在iOS的PDF文件中插入图像很容易。但有没有办法在受密码保护的PDF中插入图像。后来,我处理并解决了它 NSString *pdfFilePath1 = [self getPDFFileName]; pdfData = [NSData dataWithContentsOfFile:pdfFilePath1]; NSMutableData* outputPDFData = [[NSMutableData alloc] init]; CGDataConsu
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];
NSString*pdfFilePath1=[self-getPDFFileName];
pdfData=[NSData DATA WITH CONTENTS OFFILE:pdfFilePath1];
NSMutableData*outputPDFData=[[NSMutableData alloc]init];
CGDataConsumerRef dataConsumer=CGDataConsumerCreateWithCFData((CFMutableDataRef)outputPDFData);
CFMutableDictionaryRef attrDictionary=NULL;
attrDictionary=CFDictionaryCreateMutable(NULL、0和&kCFTypeDictionaryKeyCallBacks和&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(属性字典、kCGPDFContextTitle、@“我的文档”);
///设置密码
CFDictionarySetValue(attrDictionary,kCGPDFContextUserPassword,CFSTR(“用户密码”));
CFDictionarySetValue(属性字典、kCGPDFContextOwnerPassword、CFSTR(“ownerpassword”);
///
CGContextRef pdfContext=CGPDFContextCreate(dataConsumer,NULL,attrDictionary);
CFRelease(数据消费者);
CFRelease(字典);
CGRect pageRect;
//在pdfContext上绘制旧的“pdfData”
CFDataRef myPDFData=(uu桥CFDataRef)pdfData;
CGDataProviderRef provider=CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf=CGPDFDocumentCreateWithProvider(provider);
//将密码传递到PDF
CGPDFDocumentUnlockWithPassword(pdf,“用户密码”);
CGDataProviderRelease(提供程序);
size\u t numPages=CGPDFDocumentGetNumberOfPages(pdf);
如果(数值大于0){
//循环浏览源文件中的每个页面
对于(尺寸i=1;i