将签名图像添加到pdf而不向iOS中的用户显示pdf数据 [英] Add a signature image to a pdf without showing the pdf data to user in iOS

查看:103
本文介绍了将签名图像添加到pdf而不向iOS中的用户显示pdf数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在现有的pdf上添加签名。我通过以下方式完成了这项工作:

I want to add the signature on existing pdf. I have done this in following way:

1)在UIView上加载现有的pdf,例如mainView。

1) Load existing pdf on UIView say mainView.

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

2) Add a signature image on mainView.

3)调用以下函数

-(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一段时间,所以在新的时候调用它线程

4) This function block your UI for a while, so call it on new thread

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

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

Using above method I get a pdf data with signature which contain the old pdf data too.

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

But for above method I must need to show the pdf in UIView. If I want do the above thing without loading on UIView, without showing pdf to user, How do I do that?

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

I am able to add a Image on pdf with creating the new pdf page. How do I add a image on existing pdf?

推荐答案

我通过创建新PDF并绘制pdf数据和签名解决了这个问题在上面。请参考以下代码:

I have solved by creating the new PDF and drawing the pdf data and signature on it. Please refer following code:

// 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];

}

这篇关于将签名图像添加到pdf而不向iOS中的用户显示pdf数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆