将签名图像添加到pdf,而不在iOS中向用户显示pdf数据

我想添加现有的PDF签名。 我以下面的方式做了这个:

1)加载UIView现有的PDF说mainView。

2)在mainView上添加签名图像。

3)调用以下function

-(NSMutableData *)getPDFDatafromUIView { DebugLog(@""); // 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, mainView.bounds, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData mainView.layer renderInContext:pdfContext]; // remove PDF rendering context UIGraphicsEndPDFContext(); return pdfData; } 

4)这个函数阻塞你的UI一段时间,所以在新的线程上调用它

  [NSThread detachNewThreadSelector:@selector(launchExportViewForExportDrawing) toTarget:self withObject:nil]; 

使用上面的方法,我得到一个包含旧的pdf数据签名的pdf数据。

但是对于上面的方法,我必须在UIView中显示pdf。 如果我想在不加载UIView的情况下执行上述操作,不向用户显示pdf,我该怎么做?

我可以通过创build新的pdf页面来添加PDF图像。 如何在现有的pdf上添加图片?

我已经解决了创build新的PDF和绘制PDF数据和签名。 请参考以下代码:

 // For adding the Siganture we need to wite the content on new PDF -(void) addSignature:(UIImage *) imgSignature onPDFData:(NSData *)pdfData { NSMutableData* outputPDFData = [[NSMutableData alloc] init]; CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputPDFData); CFMutableDictionaryRef attrDictionary = NULL; attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, CFSTR("My Doc")); 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); CGDataProviderRelease(provider); CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1); pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); CGContextBeginPage(pdfContext, &pageRect); CGContextDrawPDFPage(pdfContext, page); // Draw the signature on pdfContext pageRect = CGRectMake(0, 0,imgSignature.size.width , imgSignature.size.height); CGImageRef pageImage = [imgSignature CGImage]; CGContextDrawImage(pdfContext, pageRect, pageImage); // release the allocated memory CGPDFContextEndPage(pdfContext); CGPDFContextClose(pdfContext); CGContextRelease(pdfContext); // write new PDFData in "outPutPDF.pdf" file in document directory NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *pdfFilePath =[NSString stringWithFormat:@"%@/outPutPDF.pdf",docsDirectory]; [outputPDFData writeToFile:pdfFilePath atomically:YES]; } 

我不确定我完全理解你的问题。 从我可以告诉你有一个UIView,你正在渲染到PDF文档,你想添加一个签名图像,而不是显示给用户。

如果您正在尝试将“签名图像”添加到您的PDF中,则可以将UIImageView添加到要渲染为PDF的UIView中。

如果您不希望用户同时看到UIView,则可以将其来源更改为某处,例如:

 CGRect originalFrame = mainView.frame; CGRect newFrame = mainView.frame; newFrame.origin.x = -newFrame.size.width; newFrame.origin.y = -newFrame.size.height; mainView.frame = newFrame; //do all your PDF stuff here mainView.frame = originalFrame; 

我希望有帮助。 如果没有,请澄清问题:)